|
-
March 1st, 2004, 05:30 AM
#1
Wierd output instead of 0s and 1s
A practice excercise from K&R. Kindly read the comments within the program. I'd be very grateful to people who helped. Why is it that I get the wierd face-like characters on the screen instead of the boolean output 0s or 1s?
Code:
#include <stdio.h>
/*This program is made to check that leaving the brackets around the epxression (c=getchar()) in
the statement while ((c=getchar()) !=EOF) produces a boolean output in C which would either be
0 (false) or 1 (true). This would happen because the relational operator != has precedence over the assignment
operator =.
*/
void main()
{
int c;
while (c=getchar() != EOF) putchar(c);
/* Surprisingly, it doesn't produce zeros while I type; rather it produces some wierd characters that look
half like zeros and half like smileys. And doing a Ctrl+Z, which I thought was the substitute for EOF does
not produce a 1 (true value) */
}
-
March 1st, 2004, 07:15 AM
#2
You are most likely seeing zeroes and ones on your screen. You just don't recognize them because you are expecting ASCII zeros and ones, which actually are the int values 48 and 49. 0 and 1 are unprintable ASCII characters, so it is up to your terminal how it shows them to you.
AFAIK you cannot send an EOF file from the keyboard, but usually CTRL-D sends an EOT (end-of-transmission) which should do the same.
In UNIX CTRL-Z suspends a process, don't know what it does for other OSes.
-
March 1st, 2004, 02:38 PM
#3
This question is similar to your Elementary questions from a beginner question, but since the comments are difficult to read, I am not sure how similar they are.
-
March 2nd, 2004, 10:47 AM
#4
Code:
while (c=getchar() != EOF) putchar(c);
/* Surprisingly, it doesn't produce zeros while I type; rather it produces some wierd characters that look
half like zeros and half like smileys. And doing a Ctrl+Z, which I thought was the substitute for EOF does
not produce a 1 (true value) */
From the comments above the while statement, it appears this is showing how the precedence of operators can cause problems you don't expect.
The conditional operators like != have higher precedence than the assignment operator.
Hence, what is happening here is that "getchar() != EOF" is evaluated first, mostly producing FALSE, but sometimes TRUE. Then, c is set to FALSE or TRUE, then FALSE or TRUE is put out as a character, which in ASCII on MS-DOS is usually something like the described smiley faces.
In fact, when ctrl-z is encountered, it should generate an EOF, which should cause c to be assigned to FALSE, and the loop be exitted. I don't know what is going wrong. Perhaps it's sending in a ctrl-Z instead of an EOF? Or perhaps you have to hit ENTER after ctrl-Z to force the line-by-line input parser to read and process the ctrl-Z?
The corrected code would look like this:
Code:
while ((c = getchar()) != EOF)
putchar(c);
Last edited by Gorgor; March 2nd, 2004 at 10:52 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|