The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
i have a question about one of the exercises in the k & r book....
Discuss i have a question about one of the exercises in the k & r book.... in the C Programming forum on Dev Shed. i have a question about one of the exercises in the k & r book.... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 24th, 2002, 07:16 PM
|
|
<?php
|
|
Join Date: Jun 2002
Location: Wyoming, USA
Posts: 41
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
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
|

September 24th, 2002, 07:33 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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!
|

September 26th, 2002, 08:00 PM
|
 |
An Ominous Coward
|
|
|
|
|
Curious - when would getchar() EVER return EOF? Or, is that what it returns when you hit enter?
|

September 27th, 2002, 03:08 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|