Solved

What is the difference between this n this code ..

Posted on 2004-08-06
7
223 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 500 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

Question has a verified solution.

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

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now