#1
  1. No Profile Picture
    <?php
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Wyoming, USA
    Posts
    41
    Rep Power
    13

    i have a question about one of the exercises in the k & r book....


    hey,

    i have a question for someone who has the book, "THE C PROGRAMMING LANGUAGE [2nd Edition]".......

    on page 17 and exercise 1-6 it says, "Verify that the expression getchar() != EOF is 0 or 1."

    what does this mean? how would i do this?

    thanks a lot,

    -sean
    "I speak English, can you type it?" -Everett_XML
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Basically in C, a comparision evaluates to 0 (false) or 1 (true). An if statement like this:
    if (getchar() != EOF) {
    ...
    }
    will cause the compiler to evaluate the comparision expression as a 1 (true) or a 0 (false). You could actually assign the result of the comparision to another variable, if you like, something like this:
    Code:
    int foo;
    
    if (foo = (getchar() != EOF)) {
      /* Do something */
    }
    printf("%d\n", foo);
    the value of foo will be 0 or 1 depending on whether the getchar() returned an end-of-file character or not.

    Hope this helps!
  4. #3
  5. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Curious - when would getchar() EVER return EOF? Or, is that what it returns when you hit enter?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Nope, the enter key is not the EOF char on any OS that I'm aware of. On *NIX, the EOF character is ^D and on DOS, EOF is ^Z.

IMN logo majestic logo threadwatch logo seochat tools logo