473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

}
Nov 14 '05 #1
3 1392
Sathyaish <Vi****************@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 (pa*****@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
Nov 14 '05 #2
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Sathyaish <Vi****************@yahoo.com> 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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #3
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 <pa*****@cc.helsinki.fi> wrote in message news:<c1**********@oravannahka.helsinki.fi>...
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Sathyaish <Vi****************@yahoo.com> 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."

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Markus Fischer | last post by:
Hi, I'm experiencing a wierd problem with IE 6 in Windows with two _slightly_ different Version. Give the following HTMl-Code, ideally the output of offsetTop should be "105"; a few pixel...
1
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
7
by: Shane Bishop | last post by:
I've been fighting with the Page_Load event firing twice. I looked through this user group and saw several other people having similar problems. There were various reasons for it:...
3
by: Michael Loughry | last post by:
I'm working for a company in Houston developing a web application. At one point in the code, we have to refresh the page, but save what checkboxes have been selected. Since these checkboxes are...
5
by: subaruwrx88011 | last post by:
I am very new to c++ and very new to maps. Below is the source of my program. The output is very strange. Am I inserting correctly into the map? Any help will be greatly appreciated. #include...
5
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
3
by: Cmaza | last post by:
Hi, I've been dealing with PHP for a few years now and I've never encountered a problem quite like this. I've searched the net for an answer to my problem hoping somebody else may have...
7
by: rotaryfreak | last post by:
Im working on an assignment where i need to create a class that defines old english currency. In this class i have 1 default constructor which sets my attributes, pound, shilling, and pence, to 0 and...
7
by: chromis | last post by:
Hi, I've created a utf8 encoded RSS feed which presents news data drawn from a database. I've set all aspects of my database to utf8 and also saved the text which i have put into the database as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.