-3

If a scanf statement has any white space in the format string, like as follows,

scanf(" %c",&abc);

then it just skips over an unlimited number of white spaces until it hits a character.
So \n p as input would store p in abc.

Using this concept I am not able to predict the output of the following program that I typed.

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

Note that there is a trailing space in the scanf statement.

Upon execution of the code I get the following behaviour.
It asks for a character. I enter C
It asks for a character. I enter I
It prints C.
It asks for a character. I enter R
It prints I.
It asks for a character. I enter C
It prints R.

This goes on forever. If I press newline, then it just skips it.
Why does it ask for two characters in the beginning? Shouldn't it execute the printf statement?
Why is the character of the previous input get printed in the next one?

SmarthBansal
  • 174
  • 1
  • 10
  • @xing so if there isn't any white space, then it would 'wait' for white space? – SmarthBansal Feb 23 '19 at 18:32
  • 1
    I fail to see how your posted code corresponds to your description of the behavior. I see no code asking for a character. – EOF Feb 23 '19 at 18:33
  • @EOF the scanf statement? – SmarthBansal Feb 23 '19 at 18:34
  • 1
    Possible duplicate of [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – Ilmari Karonen Feb 23 '19 at 18:37
  • @SmarthBansal `scanf()` *scans* input. It does not ask for it. – EOF Feb 23 '19 at 18:37
  • @EOF I see what you mean. By saying it asks for a character I mean that it waits for the user to enter something. – SmarthBansal Feb 23 '19 at 18:39
  • 1
    "It asks for a character" **how??** try `printf("\n==> "); fflush(stdout);` right before the `scanf()`. – pmg Feb 23 '19 at 18:41
  • @pmg I don't understand. It clearly asks for input? – SmarthBansal Feb 23 '19 at 18:54
  • 1
    Rather than a simple blinking cursor as input prompt, try my suggestion. – pmg Feb 23 '19 at 18:56
  • @pmg wow! Thanks, you solved my problem! Summarizing: It checks for non-whites only after I press *enter* during the execution of the scanf. If it doesn't find any, then it waits for the next *enter*. Right? – SmarthBansal Feb 23 '19 at 19:06
  • 1
    Kind of ... `scanf("%c ", &ch);` reads a character and skips 0 or more whitespace. You type "3ENTER" and you're still stuck inside `scanf()` ignoring whitespace. You type a few more ENTERs (TABs, SPACEs) and you're still stuck inside `scanf()`. When finally you type "7ENTER", `scanf()` gets out of ignoring whitespace (leaving the "7ENTER" in the input buffer) and the program continues. – pmg Feb 23 '19 at 19:15
  • @pmg Thanks! Should I delete this question? I now understand how poorly worded it is. – SmarthBansal Feb 23 '19 at 19:18

1 Answers1

2

In scanf format the space character matches any amount of white space (spaces, tabs and newlines) in the input.

So you enter C + newline and nothing happens, as scanf matched the character C to %c and the newline is eaten by the space. But scanf will continue until it gets something which does not match. So you can put any amount of spaces, tabs and newlines here.

When scanf reads somewhing that does not match, it calls ungetc(c, stdin) to put it back to stdin and returns. So the space will continue matching until you put something different.

That's why the last scanf returns when you type R + newline. Then the next scanf executes, it matches R with %c and the newline with space. But space is still matching any number whitespaces. It will end when you type a non white-space character.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111