Why run this code and print whole string?
#include <stdio.h>
void main()
{
int a;
while(a!='q')
{
scanf("%c",&a);
printf("%c",a);
}
}
Enter string except q, and finally press enter key. Only now your string will print on the screen. Why?
The problem here is not with scanf, it is with your printf call.
Printf buffers output until a new line is reached, so the program will not display anything until you
printf("\n");. (Which also happens when someone presses enter, you output their return to the screen which causes the buffer to flush.)If you don’t want to break up your output with
printf("\n"), then you can usefflush(stdout)to manually flush the buffer without printing anything, like this: