Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi all,

Regarding the functionality of "getchar". I read everywhere that what "getchar" does, is to read return the first character input and return it. So if I write the following:

C++
char ch;
ch=getchar();
printf("%c",ch);

... and provide "asdf qwer" as input and press "Enter", I will see that when "ch" is printed, it will return the first character of the input sequence ("a"), which makes sense.

However, if I write the following:

C++
char ch;

ch=getchar();

while (ch!=EOF)
{
    printf("c",ch);
    ch=getchar();
}

...and provide whatever sequence of characters as input (e.g "asdf qwer"), whenever I press "Enter" the "printf" prints the whole sequence of characters provided ("asdf qwer") and not just the first character ("a"). If "getchar" is supposed to read and return the first character input, wouldn't the logical conclusion be that, if this process is done iteratively (until I press Ctrl+z in the afforementioned case), it would return the first letter of the character sequence input and NOT the whole sequence? Question is: why is this happening?

Thank you in advance.
Posted
Updated 13-Jul-12 3:10am
v2
Comments
[no name] 13-Jul-12 9:15am    
You do realize that the code snippet that you provided does not do what you describe don't you?
KoGiot 14-Jul-12 13:30pm    
Before giving attitude, next time try compiling and executing
[no name] 14-Jul-12 13:56pm    
You should learn to take your own advice. I did compile and run it. How do you think I know that it does not do what you said it does? You need to learn to describe problems accurately if you want to get accurate answers.
KoGiot 15-Jul-12 7:27am    
For at least two people it was accurate enough. If this is not the case for you, then you can either provide a useful comment, or ask for a more definitive description, or not deal with the issue at all, instead of showing off.

getchar returns the first character of the remaining sequence. Some streams - the standard input for example - cannot be rewound, so when you read a character, that's it - it's gone for good. You can't undo the user and get him to retype it from the start!
 
Share this answer
 
Comments
KoGiot 14-Jul-12 13:31pm    
Thank you for your time.
OriginalGriff 15-Jul-12 2:02am    
You're welecome!
First of, your code contains two mistakes:

1. getchar does not return a char but an int. This is important, because EOF is normally defined as -1, which otherwise could not be distinguished from the character 0xff.

2. in the printf statement you probably meant:

printf("%c",ch);


Now, all that fixed, back to your question. Of course, getchar does not only return the first character of the stdin stream, but also removes it from the stream. Hence to next call of getchar will return the character.

As an exercise, try to imagine how useful getchar would be if it didn't remove the character from the stream.
 
Share this answer
 
Comments
KoGiot 14-Jul-12 13:31pm    
Thank you for your time.
nv3 14-Jul-12 13:57pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900