Question:
Learning C: cannot understand getchar()?
Sandrine
2011-03-18 13:30:32 UTC
Just started reading C programming book by K&R. I just can't understand what the EOF does compared to '\n', and how the getchar() function works. Here is a (very simple) code, that copies its input to its output one character at a time :
#include
main()
{
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}



My first question is what is the flow of the script?
My understanding is:
When I first run the script, I am in the main() function. The script wait for an input. I press "1". Once the first input is entered by pressing the key "1", the value is stored in the parameter 'c' (c=getchar()) // because c=1 is not EOF, the value of c is print on the screen (putchar(), then the 2nd input, let's say "2", is read and the value is stored in c and compared to EOF and print on the screen.

When I run the script, this is actually what I see on the cmd window: I start by the input "12'. I then pressed 'enter' key, and all the inputs are copied on the second line i.e "12", and the command prompt window waits for additional inputs on the 3rd line.
--> Why is the output printed on the screen only after I pressed "ENTER"?
There is nothing in the script above that says to print the output after "ENTER" key is pressed.

--> The way I read the script, I would have expected that the output is printed everytime I press a key. So when I press "1", I shoudl see on the screen "11" (the first "1" is the input, and the 2nd "1" is the copy from putchar()).


If I replace EOF by '\n', then same thing happen, except that instead of being on the 3rd line waiting for additional entry, the program stops and the prompt show me the adress (C:\).
Six answers:
Peter H
2011-03-18 13:42:18 UTC
This is the feature of getchar(). It waits for the Enter key to be pressed before processing the previous input. If you want each keystroke processed directly, use getch() instead.
2011-03-18 21:05:46 UTC
actually, first time when u press 1, it is stored in c variable and also put in a buffer. it happens with all next inputs (all of them r put into the buffer). when u press Enter, all the values in the buffer get printed ( getchar() has been designated in this way). it waits bcz u have not pressed ctrl+z (EOF) yet.
James Bond
2011-03-18 18:08:47 UTC
Better use getch(). However, it works under Turbo C only.

What do you want really.

Only thing I can say that your analysis correct.

getchar takes input only when we press newline. The getch is not like that.

EOF means, end of file. In DOS, Windows it is CTRL Z

while in Unix or Linux, it is CTRL D
yogesh
2011-03-18 13:46:48 UTC
To clear the concept of getchar(),i recommended to u to read the 'Let us C' by Yashavant Kanetkar.

I m also able to answer but i have to suggest u to buy 'Let us C' if u don't have it and read it .Buy it now.It will be a very usefull book.I started c++ now by self.
2016-11-16 19:52:59 UTC
I agree thoroughly. that's unquestionably one in all of the main significant flaws in faith. Many theists (which comprise Christians and, so a tactics as i comprehend, Muslims) let us know that God created certainly each little thing, and is likewise thoroughly all-powerful and omniscient. If it somewhat is unquestionably the case then each little thing that became, is or ever would be is how that's simply by fact he chosen it, and as a result human decision is an phantasm. It then follows that if each decision we make is an phantasm (simply by fact God created the universe and each little thing in it, so each little thing that happens, which comprise our 'judgements', happens simply by fact he made it gain this), then there is not any such ingredient as sturdy or undesirable. As sin would not exist, and neither does distinctive function, then no person can likely circulate to heaven or hell, simply by fact your afterlife holiday spot is desperate by utilising the style you chosen to stay your life, and you probably did no longer pick that, God did. it style of feels that the greater i seem at and picture approximately faith, the greater absurd and illogical it turns into. I additionally word that maximum arguments against your view are not logically sound, as they only misread or misrepresent your component. Does this loss of comprehension say some thing relating to the way that theist minds artwork, i ask your self? EDIT: i admire how somebody mentions that God gave Adam the alternative no rely if to settle for God or no longer. in accordance to christian theory God designed and created certainly each little thing, which comprise Adam. This would desire to comprise Adam's innovations and his determination-making expertise (if God did no longer make those, then who did?). Adam ought to as a bring about trouble-free terms probably make the determination that God programmed him to make; to make the different determination, God might would desire to have designed and geared up him in yet differently, with a distinctive innovations. that is similar to me designing and development an extremely-complicated laptop, then asking it to go with. logic dictates that the laptop can in trouble-free terms ever probably make the determination that I programmed it to make. To make a distinctive decision, i might would desire to have programmed it in yet differently, to make that decision instead.
2011-03-18 13:36:42 UTC
dios mio qe complicado q hces todo che !


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...