% c will not skip char spaces, as numeric format specifiers do. Therefore, if you use:
#include<stdio.h> int main(int argc, char* argv[]){ char c; scanf("%c", &c); printf("%c\n", c); scanf("%c", &c); // Try running with and without space printf("%c\n", c); return 0; }
It is very likely that the previous space character in the input buffer will be made in the second scanf, and you will not get a chance to type. A space up to% c will cause scanf to skip any space character in the input buffer so that you can correctly enter your input. Sometimes, to get the same effect, people write:
fflush(stdin); scanf("%c" &c);
But this is considered very poor programming, as C Standard defines the behavior of fflush (stdin) undefined. Therefore, always use a space in the format string unless you have a specific reason to capture spaces.
Codebuddy
source share