I'm reading Kernighan & Ritchie C programming book now and have a few questions about 'getchar' and 'putchar'.
1. The question is below the code example:
When I run this code in the terminal and enter a symbol (for example 'i') and than press Enter it prints entered symbol:Code:#include <stdio.h> int main(void) { int c; c = getchar(); while(c != EOF) { putchar(c); c = getchar(); } return 0; }
i // entered symbol
i // printed symbol
After that it invites me to enter another symbol. If I enter another symbol the procedure repeats. If I press Ctrl-D (EOF) instead of entering another symbol, the program terminates. The first question is why when I run this code and after entering a symbol ('i') I don't press Enter but I press Ctrl-D, it prints entered symbol and prints it in that way:
ii // the first symbol is entered, the second is printed after Ctrl-D
So to terminate the program I need to press Ctrl-D again.
2. Why in the code above I can enter a word and it prints a word and in code below it prints only one symbol even if I entered a word? The code:
Will be grateful for any help.Code:#include <stdio.h> int main(void) { int c; printf("Enter a character:\n"); c = getchar(); printf("Entered character is:\n"); putchar(c); printf("\n"); return 0; }