Answers for exercise in K & R

This is a discussion on Answers for exercise in K & R within the C Programming forums, part of the General Programming Boards category; Hey, I am currently working through Kernighan & Ritchie's 'The C Programming Language,' and have been doing the exercises they ...

Answers for exercise in K & R

Hey, I am currently working through Kernighan & Ritchie's 'The C Programming Language,' and have been doing the exercises they list at the end of the sections where you have to modify the programs or come up with your own etc..

Anyway, there are a couple that have been stumping me, (I am not in a hurry to give up on them, as I think the hard ones are the good way to learn), so I took a look around on the web to see if I could find some help, and there is a site that gives a lot of the answers to the exercises. Here is one of their answers (which does not seem to work right. It compiles ok, but does not seem to do what I want it to, or even what is listed in the comments at the beginning of the code):

Code:

/* This program prompts for input, and then captures a character
* from the keyboard. If EOF is signalled (typically through a
* control-D or control-Z character, though not necessarily),
* the program prints 0. Otherwise, it prints 1.
*
* If your input stream is buffered (and it probably is), then
* you will need to press the ENTER key before the program will
* respond.
*/
#include <stdio.h>
int main(void)
{
printf("Press a key. ENTER would be nice :-)\n\n");
printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF);
return 0;
}

I am pretty new to C, but as I looked at this, I can't see how the second printf statement could work, if there is no variable initialised.

>>I can't see how the second printf statement could work, if there is no variable initialised.
An expression can be used anywhere a variable can, so the return value of a function tested for boolean true/false is perfectly legal.

>>but does not seem to do what I want it to
That's usually the case. :-)

>>or even what is listed in the comments at the beginning of the code
It does exactly what it's supposed to, print 1, or true, if you type anything except ctrl+z or ctrl+d, where it prints 0, or false.

The function getchar() will return a value, this value is compared with EOF. In case the returned value is equal to EOF, the comparison will evaluate to FALSE. In case the returned value is not equal to EOF, the comparison will evaluate to TRUE. The result of the comparison, FALSE or TRUE, is the value to be printed by printf at the place of %d.

Ok people, thanks for the comments. Actually, I do know there is an answer book for K&R and I would not mind having it, but I don't have the liberty to spend the $60 CDN on it right now. I was pleased to find some sort of solutions on the net anyway, so I was not having to wait for a long time for answers.

When I compiled the program on the Red Hat system that I host my domain with, the program did not work as it should have, hence my questions here. I did compile it on my own system, and things seem to look fine on this one.