2

I tested this code:

  1 #include <stdio.h>
  2 
  3 main()
  4 {
  5     int c;
  6 
  7     while ((c = getchar()) != EOF) {
  8         putchar(c);
  9         printf("%d ", c);
 10     }
 11     printf("%d\n", c);
 12 }

Question:

When I inputted a line of characters, and then inputted an 'enter', I got this kind of result:

asdf

a97 s115 d100 f102

When I added an EOF(ctrl+d) directly behind a line of characters, I got the result directly behind the input, like:

asdfa97 s115 d100 f102

My questions are whether the 'enter' triggered the code running? Why when I input an EOF, was not the 'enter' needed to output the result? Why did I need another EOF to quit running?

Thanks a lot.

microbit
  • 319
  • 3
  • 10
  • Your terminal adds EOF when you submit a line. – Quentin Jun 26 '14 at 22:45
  • Buffered input. What you write is not actually consumed by `getchar()` until you hit return or feed it an `EOF`. When you hit return, the console simply moves the cursor to the next line, whereas with `EOF` it doesn't. That's just it. – Filipe Gonçalves Jun 26 '14 at 22:46
  • I think you're not including all of the output. I'll write up an answer. – Carl Norum Jun 26 '14 at 23:31
  • Quentin and Filipe - OP's terminal is almost certainly not sending EOF in *either* case. – Carl Norum Jun 26 '14 at 23:40
  • Very closely related to [Canonical vs non-canonical terminal input](http://stackoverflow.com/questions/358342/canonical-vs-non-canonical-terminal-input/). – Jonathan Leffler Jun 27 '14 at 00:29

1 Answers1

1

For your first case, are you sure the output wasn't:

asdf
a97 s115 d100 f102 
10 

That is, your input line asdf followed on the next line by the output characters and numbers for 'a', 's', 'd', and 'f', and then another line (because you putchar() the newline character, too) with a 10 (the ASCII value for a newline character) on it?

Note that your program doesn't exit at this point either - it's still waiting for more input.

^D is not inputting an EOF "character", either. It's just sending a signal to your terminal program. In your case, it looks like it means "flush buffers", so your program gets access to the terminal's line-buffered input of "asdf". Since your program doesn't output a newline, you get the output on the same line.

If you enter the ^D on a line by itself, you'll cause the terminal to close its connection to your program and the actual EOF will come through, terminating your progam.

Example - input "asdf\n":

# ./example 
asdf
a97 s115 d100 f102 
10

Example - input "asdf^D":

$ ./example 
asdfa97 s115 d100 f102

Example - input "asdf\n^D":

$ ./example 
asdf
a97 s115 d100 f102 
10 -1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Thanks for your answer. You are right, there is an output '10'. I appreciate that you also help me understand the '10' output:) – microbit Jun 27 '14 at 01:27
  • Every time the putchar() is called, there will be a new line, right? For example, if there are three putchar(), the '10' will appear in the third line counted from the result line? – microbit Jun 27 '14 at 01:33
  • The `10` is the ASCII value for the newline character you typed. It gets output exactly the same as all your other input does, it's just that you can't see the newline - it makes a new line, after all. – Carl Norum Jun 27 '14 at 15:04
  • And no, there will not be a new line each time `putchar()` is called. If you pass it a newline character, it will output a newline character, but it won't add any extra ones in for you. – Carl Norum Jun 27 '14 at 15:04