Input:
malik
MrJupiter
Output;
malik
malik
I know that fflush(FILE * stream) is used to clear the buffer, but in this example, since the first scanned input is "malik\n" than the buffer (for the first time) is loaded with the String "malik\n" and cleared after the print (that what I think...). In the second iteration, since the buffer is empty (it seems that it isn't empty, because the second output is the same as the first one) the program must return "MrJupiter\n".
Can someone explain to me how the buffer have been manipulated in this case?
#include <stdio.h>
#include<stdlib.h>
int main()
{
char str[20];
int i;
for (i = 0; i<2; i++)
{
scanf("%[^\n]s", str);
printf("%s\n", str);
// used to clear the buffer
// and accept the next string
fflush(stdin);
}
return 0;
}