Showing that an integer function's value and the EOF value are not the same I was doing Exercise 1.6 and 1.7 in 'The C Programming Language' by K&amp;R.

The questions state: "Verify that the experssion getchar() != EOF is 0 or 1" (Exercise 1.6) and "Write a program to print the value of EOF" (Exercise 1.7)

Here is my attempt at trying to do both in one program:

#include &lt;stdio.h&gt;

int main()
{
    printf("%d \t This is the value of EOF \n", EOF);
    int c = getchar() != EOF;
    printf("%d \t This is the value of getchar(), our variable \n", c);
}


I was wondering if there is a more concise or preferred way to do this?
The original code from this question improperly assumes that c still has the numeric value of a character, saying "This is the value of getchar()".  This is not actually true based upon that code.  The value being printed out in the last line is not the value of getchar() but the value of the comparison of getchar against != EOF.  

To clarify things, I wrote the below code, with comments.

#include &lt;stdio.h&gt;

int main( void )
{
  /* result variables, with initial value for cleanliness */
  int getchar_result = 0;
  int compare_result = 0;

  /* print out the value of EOF as an integer */
  printf( "%d \t This is the value of EOF \n", EOF );

  /* obtaining the result of getchar */
  getchar_result = getchar();

  /* obtaining the result of comparison */
  compare_result = EOF != getchar_result;

  /* print out the actual value of the getchar_result, which may or may not be EOF */
  printf( "%d \t This is the value of getchar()\n", getchar_result );

  /* print out the value of the comparison result against EOF */
  printf( "%d \t This Is the result of (getchar()!=EOF)\n", compare_result);
}


This produced the results, when ctrl+D was entered during the execution the first round, and 1 was entered during the second.  Notice that the comparison value is zero (not true) in the first, and 1 (true) in the second.

UbuntuPrompt&gt;./a.out
-1       This is the value of EOF
-1       This is the value of getchar()
0        This Is the result of (getchar()!=EOF)
UbuntuPrompt&gt;./a.out
-1       This is the value of EOF
1
49       This is the value of getchar()
1        This Is the result of (getchar()!=EOF)


Hope this helps.