How does buffer work using printf and scanf?

Asked

Viewed 909 times

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.

1 answer

1

Hello friend the problem is that when reading a string with more than one word (with spaces) it stops to read by storing only the first word. An example would be the string "My house", if we use the above syntax it will only store the word "My", stopping to find the space.

What we can do to fix this error is to force scanf to read the string until it finds [enter], for this we must insert the following code:

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

The parameters passed between the scanf or printf quotes are read or output properties that C provides. In your code it is only stating that it will read a string and give a line break after the variable.

\n = quebra de linha

To know how broad this is, the example below will read all letters of the alphabet ignoring typed numbers!

scanf("%[a-z A-Z]s");
  • Yes, I’ve seen it in many books, but without the s after the clasps. What this s influences ? And how buffer works in this case, it reads up to enter and leaves enter in the buffer or captures it ?

  • So what’s your real question? Hugs

  • How the buffer reacts with scanf formats %c %s %[^\n] ?

  • 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

  • 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 to scanf, in this case, as stated string[50], string (pure, without &), is already the address.

  • 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 as string.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.