I have read in an earlier discussion that
#include <stdio.h>
int main ()
{
int num;
printf("\nPlease enter an integer:\n");
scanf("%d",&num);
// Let us check whether the user input is in integer //
while ((num = getchar()) != '\n' && num != EOF)
{
printf("\n\nError in input\n");
printf("Please enter valid integer\n");
scanf("%d",&num);
}
}
will check whether the input is an integer or not. The code works. But I do not understand what
while ((num = getchar()) != '\n' && num != EOF)
is doing. Can anyone help?
scanf. And there is no need to be fumbling withgetchar.numasint, so it will always be anint. Additionally, yourscanf()only reads integers with%d. You need to rethink your code entirely.!=having higher precedence than&&and the fact that the&&operator comes with a built-in sequence point that guarantees left-to-right evaluation. And they assume that you naturally understand that getchar() gets an int, not a char. To show such code to newbies and expect them to understand, is like pushing kids into the deep end of the pool so that they may learn to swim.