advanced printf() and scanf()

G

Gaijinco

I read somewhere that printf and scanf had "advanced features" and they
point to:

scanf("%[^\n]",line); // line is a string

as an example, where scanf() acts like gets()

I try to look of more of this "advanced features" in documentation but
couldn't found any. What "advance features" do you of these functions
(printf and scanf)?

Thanks a lot!
 
V

Vladimir Oka

Gaijinco said:
I read somewhere that printf and scanf had "advanced features" and they
point to:

scanf("%[^\n]",line); // line is a string

This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.
as an example, where scanf() acts like gets()

Making anything behave like `gets()` must be EVIL!

Think of what happens when there's more charaters in the input buffer
than you allowed for in your string buffer.

You're better off using `fgets()` and `sscanf()`.
I try to look of more of this "advanced features" in documentation but
couldn't found any.

Try the C Standard. You should be able to download the current version
of C99 plus TCs as N1124.pdf. Google for it.
What "advance features" do you of these functions (printf and scanf)?

In my line of work, exactly none (don't even use the functions, let
alone their "advanced" features).
 
R

Richard Tobin

Gaijinco said:
I try to look of more of this "advanced features" in documentation but
couldn't found any. What "advance features" do you of these functions
(printf and scanf)?

I don't know if you'd call it an "advanced feature", but I have seen
examples that would have been a lot simpler if someone had known about
the use of "*" as the precision modifier.

Suppose that "s" points to an array of characters whose length is
"len" - a representation of a string that does not use null-termination.
This can be printed with

printf("%.*s", len, s);

but I have seen programs where the author malloced a buffer, copied
the string, and null-terminated it so that he could use "%s".

-- Richard
 
W

Walter Roberson

Vladimir Oka said:
Gaijinco said:
I read somewhere that printf and scanf had "advanced features" and they
point to:
scanf("%[^\n]",line); // line is a string
This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.

As the scanf() form leaves the newline in the input stream, but
the gets() form does not, the two are not equivilent.

You could use:

scanf("%[^\n]\n",line);

but you should be sure you understand the result you would get
if end of file occurs before you encounter a \n .
 
M

Michael Wojcik

As the scanf() form leaves the newline in the input stream, but
the gets() form does not, the two are not equivilent.

You could use:

scanf("%[^\n]\n",line);

but you should be sure you understand the result you would get
if end of file occurs before you encounter a \n .

Also, since \n is a whitespace directive for the fscanf family, it
will consume not only the newline (if it exists) but any following
whitespace. If this scanf is used in a loop, for example, it will
remove leading whitespace from all lines after the first. So again,
this is not equivalent to gets().

(It *is* similar to gets() in that, as presented, it can overflow
the line object. There should be a maximum size specifier on that
conversion directive.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top