0

Let's say that I use scanf to, for example, read a character from the keyboard. After that I use printf to print the character I just read.

scanf("%c",&ch);
printf("%c",ch);

When scanf is reading the character, I must press enter to continue and run the printf, right?

And let's say I enter ABCD with the keyboard. After that printf will print A.

But when I do this:

do {
   scanf("%c",&ch);
   printf("%c",ch);
} while (ch!='\n');

and enter ABCD with the keyboard, I assume that the printf must print A. And because A is not \n it will continue the loop, right?

But instead of this it will print ABCD. Why does this happen?

HonkyTonk
  • 1,961
  • 11
  • 11
user1809300
  • 703
  • 4
  • 9
  • 17

3 Answers3

7

When you type in "ABCD\n", each scanf("%c",&ch); reads one char from the input buffer, until the newline is reached.

So after the 'A' is printed, there is still a "BCD\n" in the buffer, so that the next scanf immediately succeeds reading another char, 'B' in the next iteration of the loop.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • but scanf supposed to keep the first digit only – user1809300 Nov 13 '12 at 16:43
  • But you have a loop, and several `char`s in the input buffer, so the next `scanf` reads the next `char` from the buffer, until that is empty or a newline is found. – Daniel Fischer Nov 13 '12 at 16:43
  • and if i want to keep only the first letter from input buffer and in the next loop i want to keep the first letter from another sequence ? – user1809300 Nov 13 '12 at 16:46
  • @user1809300 If you want to read first character of a line, you need to read the first character and then read+discard the rest of the line. – Šimon Tóth Nov 13 '12 at 16:49
  • 1
    Then you need to clear the input buffer after each scan, `int c; while((c = getchar()) != EOF && c != '\n'); if (c == EOF) exit(EXIT_FAILURE);`. Or something similar. – Daniel Fischer Nov 13 '12 at 16:49
  • @user1809300 Do note, that you can have multiple lines in the buffer, not just one. – Šimon Tóth Nov 13 '12 at 16:50
  • ok thank you...adn what do you mean multiple lines in buffer? – user1809300 Nov 13 '12 at 16:51
  • and another thing : when i dont have a loop and scanf ABCD why does it keep only the first digit without reaching a newline /n – user1809300 Nov 13 '12 at 16:56
  • @user1809300 Because you are reading one character. What the loop does it that you read one character four times (edit: five times). – Šimon Tóth Nov 13 '12 at 17:00
  • 1
    Without a loop, only one `scanf("%c", &ch);` is executed, so it takes only one character from the input buffer. If you do something like `char buffer[100]; fgets(buffer, sizeof buffer, stdin); printf("%s\n", buffer);`, you will see that the other characters you typed were still in the input buffer. – Daniel Fischer Nov 13 '12 at 17:00
4

in scanf i must press enter to continue and run the printf right?

Nope. As long as there is a character to be read, it will be read.

i put in scanf ABCD after that printf will print A ...

If you input ABCD and enter, the input will now contain five characters. A, B, C, D and a newline. Your cycle will read the characters A, B, C, D in sequence and then read the newline.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

scanf does not wait for you to press enter, it simple tries to read in what you typed if it matched your format string. If you had used %s, then it would wait until a whitespace character before matching.

This thread may also be useful: why does scanf not wait for user input after it fails one time?

Community
  • 1
  • 1
kcbanner
  • 4,050
  • 1
  • 25
  • 18