Say I have the following...
int main () {
char name [5] = "";
char c;
printf("Enter a name: ");
fgets(name,5,stdin);
while ((c = getchar()) != '\n');
printf("Exiting...);
getchar();
return 0;
}
The while loop will clean the stdin buffer but I have seen the loop done like this as well...
while ((c = getchar()) != '\n' && c != EOF);
I am wondering if there is any difference between the 2??? Does testing for EOF make any difference?
fgets
reads thenewline
anyway, what is there left to read?char name [5];
it is quite possible there is more to read, but you only need to clear the input if there is nonewline
at the end ofname
.fgetc()
returnsint
notchar
.getchar()
instead offgetc()
:)