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

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    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.

    Any comments?

    kermit

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well it should work. You could put a variable in there, but you can also test the condition of the return of a function.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>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.
    *Cela*

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF);

    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.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Don't you know there is an answer book to the K & R text? It is called the C Answer Book. ISBN 0131096532. Check it out
    Mr. C: Author and Instructor

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>there is an answer book
    But you can't have a discussion with an answer book. IMHO, the web (ie this site and others like it) are the place to get good answers for those type of questions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    >>there is an answer book
    But you can't have a discussion with an answer book. IMHO, the web (ie this site and others like it) are the place to get good answers for those type of questions.
    I agree- but I just wanted to point out the solutions to to book do exist.
    Mr. C: Author and Instructor

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.

    Anyway, again, thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yahoo Answers Type Website
    By Peckle in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-21-2008, 11:30 AM
  2. analysis and answers to ACM ICPC?
    By George2 in forum C Programming
    Replies: 0
    Last Post: 08-03-2006, 05:37 AM
  3. answers for Ivor Horton's Beginning C++?
    By 7stud in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 12:13 AM
  4. Im A C++ Newb Looking For Answers
    By gimmpy224 in forum C++ Programming
    Replies: 6
    Last Post: 05-03-2004, 04:53 PM
  5. need answers! (Only have 10 minutes)
    By minime6696 in forum Windows Programming
    Replies: 4
    Last Post: 09-01-2001, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21