1

In my C programming book, if I do not input a real char, the statement getchar() != EOF will return 0. But when I practiced this principle, I found it did not make sense according to my result. My code always return 1 no matter what character I give. Is there anything wrong with my code?

Here is it:

#include <stdio.h>

main()
{   
    printf("%d\n", getchar() != EOF);
}

1 Answers1

3

My code always return 1 no matter what character I give.

That’s the expected behaviour: getchar() should only return EOF if no character was read. In all other cases it should return the character read, with a value ≠ EOF.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214