2
Using the printf
, when I do:
printf("\n\tQualquer coisa\n");
It inserts this first into a buffer and then prints to the screen (standard output) ?
Using the scanf
format %c
, it captures from buffer character to character. But when I use the format %s
? It stops when it finds a space or enter, but it leaves that enter or space in the buffer ? If so, how can you read something from the buffer and not capture it ?
And in the case of these scanf
:
scanf("%c\n", &caracter);
scanf("%s\n", string);
These scanf
above read a buffer character (for %c
) and a string (for %s
) and remove from the buffer the next enter ? What happens ? What do these characters in quotes mean? I always imagined that in the scanf
, the first parameter was only the formats to be read.
I wish you could explain it to me in detail, because in class and in books, the explanations are superfluous, and I know it’s not quite simple how this works. If there is any documentation that reports well the behavior of this, please pass me the link, because I searched the standard of C and could not find.
Grateful.
Yes, I’ve seen it in many books, but without the
s
after the clasps. What thiss
influences ? And how buffer works in this case, it reads up to enter and leaves enter in the buffer or captures it ?– ViniciusArruda
So what’s your real question? Hugs
– vmontanheiro
How the buffer reacts with scanf formats
%c %s %[^\n]
?– ViniciusArruda
with %c you take 1 char, with %s you take string ignoring the spaces as mentioned above. %[ n] you take whitespace without any other value, if by chance you need to pick string or integer you can not just with that %[ n]. If you use scanf("%s n", &string); with & you refer to an address, where the string will have to be declared that way string[50] for example. Hugs
– vmontanheiro
Friend, first you didn’t answer my question, second you’re "teaching me" how to use pointers the wrong way. What are you doing in
scanf("%s\n", &string);
is to pass the address toscanf
, in this case, as statedstring[50]
,string
(pure, without &), is already the address.– ViniciusArruda
I just compiled and took a print to show you http://i.imgur.com/Dxpvoco.png . Maybe if you want to put
&
, you can use it like this:&string[0]
which for one-dimensional vectors is the same asstring
.– ViniciusArruda