I usually clear the stdin buffer with this code:
while(getchar() != '\n');
But most tell me that it is more advisable to do it like this:
while((ch = getchar()) != '\n' && ch != EOF);
Why do they add the ch != EOF? That's what I can't understand. What consequences would the way I do it bring?
Using the second makes validation more practical since apart from verifying if it is a line break, it also verifies that it is an End Of File ( EOF ) to exit the cycle.
In a nutshell the code basically skips all characters until the next newline.
Therefore it is recommended to use the second