0
\$\begingroup\$

I was doing Exercise 1.6 and 1.7 in 'The C Programming Language' by K&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 <stdio.h>

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ int c = getchar() != EOF; Doesn't do what you think it does. c will only contain 0 or 1, but not what was read from input. \$\endgroup\$ – πάντα ῥεῖ Apr 22 '18 at 6:15
  • 1
    \$\begingroup\$ Actually,, in this instance, @πάνταῥεῖ the code is right according to the requirements, but the string printed is misleading (not enough to be not-working code) \$\endgroup\$ – rolfl Apr 22 '18 at 10:12
  • \$\begingroup\$ @rolfl As is that code won't work like intended, did I miss something? \$\endgroup\$ – πάντα ῥεῖ Apr 22 '18 at 10:24
  • 2
    \$\begingroup\$ @πάνταῥεῖ - the code is supposed to print out the result of getchar() != EOF, and it is not supposed to print out the value of getChar(). The message should say something like This is the value of getchar() != EOF instead of This is the value of getchar() \$\endgroup\$ – rolfl Apr 22 '18 at 10:29
  • \$\begingroup\$ @rolfl "This is the value of getchar() != EOF" as it doesn't do this it's not working as intended. Sorry for nitpicking. \$\endgroup\$ – πάντα ῥεῖ Apr 22 '18 at 10:32
3
\$\begingroup\$

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 <stdio.h>

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>./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>./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.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Actually, nowhere in the question does it assume that the system uses ASCII. I'd re-word that first sentence to simply say numeric character code or something. \$\endgroup\$ – Toby Speight Nov 9 '18 at 11:24
  • \$\begingroup\$ interesting point. the man-page for getchar doesn't say ascii, but just says character. I have yet to see a computer that didn't use ascii for a character, but I suppose, technically, you are correct. \$\endgroup\$ – Keitai Otaku Nov 12 '18 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy