Where does `getchar()` store the user input?

后端 未结 7 2107
情深已故
情深已故 2020-12-16 04:25

I\'ve started reading \"The C Programming Language\" (K&R) and I have a doubt about the getchar() function.

For example this code:

#         


        
相关标签:
7条回答
  • 2020-12-16 05:17

    For your updated question, in the first example, only one character is read. It never reaches the EOF. The program terminates because there is nothing for it to do after completing the printf instruction. It just reads one character. Prints it. Puts in a newline. And then terminates as it has nothing more to do. It doesn't read more than one character.

    Whereas, in the second code, the getchar and putchar are present inside a while loop. In this, the program keeps on reading characters one by one (as it is made to do so by the loop) until reaches reaches the EOF character (^D). At that point, it matches c!=EOF and since the conditions is not satisfied, it comes out of the loop. Now there are no more statements to execute. So the program terminates at this point.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题