Solved

What is the difference between this n this code ..

Posted on 2004-08-06
7
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Suggested Solutions

Title # Comments Views Activity
Inorder binary search tree 5 186
Global Keyboard Hooks Blocked 4 104
nested if statement in excel help 4 50
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 92
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 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