Link to home
Start Free TrialLog in
Avatar of amankhan
amankhan

asked on

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;
}
 
Avatar of sunnycoder
sunnycoder
Flag of India image

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
Avatar of lemmeC
lemmeC

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.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
\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.
EOF can be supplied from stdin too ... ctrl D
>> EOF can be supplied from stdin too ... ctrl D

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