Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is the difference between this n this code ..

Posted on 2004-08-06
7
Medium Priority
?
230 Views
Last Modified: 2010-04-15
What is the difference between this
 
int main()
{
 char c;
 while ( (c = getchar()) != EOF )  ----------Line 1
  putchar(c); ------------- Line 2
 
return 0;
}
 
and this
 
int main()
{
 char c;
 while ( (c = getchar()) != EOF )  ----------Line 1
 
return 0;
}
 
0
Comment
Question by:amankhan
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11735404
Hi amankhan,

first code wll print all the character it reads ... and then return
second code will read only one char and return without printing anything

Sunnycoder
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11735420
In the first case, each of the characters given as input is echoed back. This doesnt happen in the second case.

Eg:
If you type "abc", you will get "aabbcc" in the first case, and just "abc" in the second case.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 11735441
amankhan,

>  while ( (c = getchar()) != EOF )  ----------Line 1
in the loop .. as long as we dont get end of file, keep reading one char into c
>   putchar(c); ------------- Line 2
in the loop ... print the char we just read
> return 0;


>  while ( (c = getchar()) != EOF )  ----------Line 1
loop says read till we get end of file but since first line of loop is return, it will return after reading only one char
> return 0

Sunnycoder
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:lemmeC
ID: 11735476
\n is a better option to using EOF if the intention is to get a single line from standard input.
EOF is used to while getting input from files.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11735492
EOF can be supplied from stdin too ... ctrl D
0
 
LVL 5

Expert Comment

by:lemmeC
ID: 11735528
>> EOF can be supplied from stdin too ... ctrl D

Oh, didn't know that. Is this true on DOS/Windows too?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11735536
I guess it is .. not sure though, not really a windows/dos programmer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question