1

How does scanf() hold the output of printf from appearing on the screen? The loop must be iterating because i's value is increasing as shown by the output. To clarify, how does the loop iterate without displaying the output of printf?

Code:

char string[100] = {0};
char chr = 0;
int i = 0;
printf("Please enter the string you would like reversed:\n");
while (chr != '\n'&& i<99)
{
    scanf("%c", &chr);
    string[i] = chr;
    printf("%d %c\n", i,chr);
    i++;
}

Output:

Please enter the string you would like reversed:
This is a test
0 T
1 h
2 i
3 s
4
5 i
6 s
7
8 a
9
10 t
11 e
12 s
13 t
14


Your reversed string is:
tset a si sihT
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
0x41414141
  • 364
  • 3
  • 16

2 Answers2

1

it is because the output gets flushed to the output window only after you press enter, till then it is buffered

The stdout stream is buffered, so will only display what's in the buffer after it reaches a newline (or when it's told to). You have a few options to print immediately:

Print to stderr instead using fprintf:

fprintf(stderr, "I will be printed immediately");

Flush stdout whenever you need it to using fflush:

printf("Buffered, will be flushed");
fflush(stdout); // Will now print everything in the stdout buffer

for a detailed explanation:

Why does printf not flush after the call unless a newline is in the format string?

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
1

This problem appears because stdin is buffered.

scanf() does not hold the output of printf from appearing. It is that stdin is shown as This is a test\n before scanf() begins consuming the chars.

The input is not available to scanf() until you press enter (\n). The 1st call to scanf("%c", &chr) then takes 1 char out. The 2nd call to scanf() need not wait because data is available from stdin. The chars "This is a test\n" are read, one at a time, by scanf("%c", &chr).

Had code looped again and called scanf("%c", &chr), the process would repeat.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • But isn't the printf encountered before the second loop? – 0x41414141 Dec 20 '13 at 23:57
  • @bh3244 `printf("Please...` is encountered before the first loop printfing "Please enter ...". You enter in "This is a test\n" which is echoed on the console. Then after seeing the `\n`, data is available to `scanf()`. Then the multiple `printf("%d %c\n", i,chr);` occur. – chux - Reinstate Monica Dec 21 '13 at 00:01
  • But the scanf() is only reading and storing one character at a time, so it must loop in order to store the next, but that printf("%d %c\n", i,chr) is in the way. Does the stdout wait until the stdin is gone before outputting? If so, what forces this,the compiler, the OS? – 0x41414141 Dec 21 '13 at 00:09
  • @bh3244 No, the following is not quite correct: "scanf() is only reading and storing one character at a time". The first occurrence of `scanf()` says it want a `char` from `stdin`. `stdin` is _buffered_. It does not provide any `char` back to `scanf()` until is has a `\n` as in "This is a test\n". So `scanf()` waits until the `\n` arrives. Then `scanf()` takes 1 `char` out of `stdin` (`T`) and returns. On the second call, an `h` is ready and waiting and then it gets that immediately. – chux - Reinstate Monica Dec 21 '13 at 00:15