Solved

What is the difference between this n this code ..

Posted on 2004-08-06
7
222 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

757 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

13 Experts available now in Live!

Get 1:1 Help Now