• C

What is the difference between this n this code ..

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;
}
 
amankhanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sunnycoderConnect With a Mentor Commented:
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
 
sunnycoderCommented:
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
 
lemmeCCommented:
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
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
lemmeCCommented:
\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
 
sunnycoderCommented:
EOF can be supplied from stdin too ... ctrl D
0
 
lemmeCCommented:
>> EOF can be supplied from stdin too ... ctrl D

Oh, didn't know that. Is this true on DOS/Windows too?
0
 
sunnycoderCommented:
I guess it is .. not sure though, not really a windows/dos programmer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.