Re: Wierd output instead of 0s and 1s

From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 03/01/04


Date: 1 Mar 2004 10:41:03 GMT

Sathyaish <VisualBasicLearner@yahoo.com> scribbled the following:
> 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?

Because 0 and 1 are not the same thing as '0' and '1'. Your program
is printing the characters whose character code is 0 and 1, not the
characters '0' and '1' themselves.

> #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()

Non-standard form of main(). Better would be int main(void).

> {

> 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) */

Note that if c==EOF, the putchar(c) is never called, so your program
will never print a 1 (true value) for any input.

> }

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
   - Kent Brockman