0

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?

7
  • 1
    The very first essential thing is to check the return value from scanf. And there is no need to be fumbling with getchar. Commented Jun 18, 2020 at 11:46
  • 5
    Read a full line. Try to parse the line. If it fails try again. Commented Jun 18, 2020 at 11:48
  • 1
    You defined num as int, so it will always be an int. Additionally, your scanf() only reads integers with %d. You need to rethink your code entirely. Commented Jun 18, 2020 at 11:57
  • 1
    The example is very bad. Don't use it. Follow comment #2 - get string from user and verify whether it is integer or not. Commented Jun 18, 2020 at 12:04
  • 1
    The code originates from the bad K&R book which took pride in writing code in the most obscure way possible. The original code takes advantage of != 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. Commented Jun 18, 2020 at 12:34

2 Answers 2

1

scanf("%d",&num); will parse the integer present in stdin buffer if it is indeed parseable, characters that are not parseable will remain in the buffer.

The trick in this code is to check, after the scanf, if the character present in the buffer is a newline (\n), if it is, the value was parsed correctly, in that case the loop asking for new input will not be executed, if not, the non parsed character will still be in the buffer, will be read by getchar which will verify that indeed it's not \n, and the cycle will be executed.

The problem with this is that if you input something like "aadd" the cycle will execute 4 times, the number of characters that remained in the input buffer.

Though it, more or less works, it's not the best method, as stated in the comments, it's best to read the line from the buffer with something like fgets, parse it, for example, with sscanf or strtol and if it fails, ask for new input.

Sign up to request clarification or add additional context in comments.

Comments

1

while ((num = getchar()) != '\n' && num != EOF)

is just a needlessly obfuscated way of writing:

num = getchar();
while((num != '\n') && (num != EOF)) // inner parenthesis not necessary, just for readability
{
  ...
  num = getchar();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.