Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Wierd output instead of 0s and 1s

 
Thread Tools Search this Thread
Old 03-01-2004, 10:33 AM   #1
Default 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?

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

}


Sathyaish
  Reply With Quote
Old 03-01-2004, 10:41 AM   #2
Joona I Palaste
 
Posts: n/a
Default Re: Wierd output instead of 0s and 1s

Sathyaish <> 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 () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
  Reply With Quote
Old 03-01-2004, 10:43 AM   #3
Joona I Palaste
 
Posts: n/a
Default Re: Wierd output instead of 0s and 1s

Joona I Palaste <> scribbled the following:
> Sathyaish <> scribbled the following:
>> #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);


This loop is printing the character whose code is 1 (true value)
as long as getchar()!=EOF. Note that 1 and '1' are not the same thing.

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


>> }


I meant, of course, "if getchar()==EOF and therefore c==0, the
putchar(c) is never called, so your program will never print a 0 (false
value) for any input."

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
  Reply With Quote
Old 03-01-2004, 11:04 AM   #4
Mark L Pappin
 
Posts: n/a
Default Re: Wierd output instead of 0s and 1s

(Sathyaish) writes:

[rearranging for context]
> #include <stdio.h>
> void main() {
> int c;
> while (c=getchar() != EOF) putchar(c);


> Why is it that I get the wierd face-like characters on the screen
> instead of the boolean output 0s or 1s?


short:
Probably because '0' != 0 and '1' != 1, but maybe because 'void
main()' invokes undefined behaviour.


longer:
You call putchar() with the result of the comparison, which is indeed
0 or 1. On your platform (and the IBM PC BIOS uses this mapping),
characters 0 and 1 are smiley faces. If instead you use
putchar('0'+c)
you should see some of the results you expect.

Note that 'void main()' is not correct. Main must return int, or
anything at all is allowed to happen - the fact that things seem to
behave fairly normally is only one possible interpretation of
'anything at all'.


mlp
  Reply With Quote
Old 03-02-2004, 09:16 AM   #5
Sathyaish
 
Posts: n/a
Default Re: Wierd output instead of 0s and 1s

Thank you so very much, Joona Palaste. Yeah, I realize I flipped the
zeros and ones in writing the original post.

You're de man!

Kind Regards,
Sathyaish Chakravarthy.



Joona I Palaste <> wrote in message news:<c1v44d$8og$>...
> Joona I Palaste <> scribbled the following:
> > Sathyaish <> scribbled the following:
> >> #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);

>
> This loop is printing the character whose code is 1 (true value)
> as long as getchar()!=EOF. Note that 1 and '1' are not the same thing.
>
> >> /* 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.

>
> >> }

>
> I meant, of course, "if getchar()==EOF and therefore c==0, the
> putchar(c) is never called, so your program will never print a 0 (false
> value) for any input."

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump