I have a test code like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char c, str[20];
printf("Enter a character : ");
scanf("%c", &c);
printf("Enter a string : ");
fflush(stdin);
gets(str);
printf("\n\n\nResult : %c\n%s\n", c, str);
return 0;
}
I've read some articles said that this code will work because after scanning the c character, there is still '\n' character in the buffer. The fflush(stdin) will clear the buffer, so gets() function can work properly
But in fact, when I compile and run this code in Mac OS environment, fflush(stdin) do nothing. I entered a character (for ex, 'k'), then it print the k character and a '\n' character. It supposes to allow me to enter a character, a string and then print both of them. Anyone know why? Thanks!