0

When I use this code, I can type into the command line and get back what I typed:

main() {
  int c;
  while ((c = getchar()) != EOF) {
    putchar(c);
  }
}

Output:

~/code/c $ ./a.out
one
one
two
two

But when I use this code, it only works when I pipe in data, not when I type data into the command line:

main() {
  int nc;
  nc = 0;
  while (getchar() != EOF) {
    nc++;
  }
  printf("%d\n", nc);
}

When I pipe data in from a text file:

~/code/c $ ./a.out < practice.txt
14

When I try to input data from the command line:

~/code/c $ ./a.out
one
two
three

What's going on?

3
  • 3
    You haven't told the program there's no more data (sent it EOF), so it is waiting for more input. You don't print the length for each line; only for the whole file. Since you're probably on a Unix-like machine, you should probably type Control-D at the start of a line to see the final answer. If you type the Control-D part way through the line, you will need to type a second Control-D to indicate EOF. Aug 29, 2015 at 2:13
  • Why does my first code example behave the way it does? I'd expect that after each keypress, it would print the character. What's happening is that it spits back the characters I pressed after pressing enter. Why is this? Aug 29, 2015 at 3:26
  • 1
    The terminal driver doesn't send the characters to a program until you type newline (or Control-D). This allows you to edit the line (backspace, etc). So, unless you change the terminal settings, your program gets one line of data each time you hit return. Hence the observed behaviour. Aug 29, 2015 at 3:29

2 Answers 2

3

The while loop never exits since while (getchar() != EOF) is always true. After you're done with the input, press Cntrl+D for Linux or Ctrl+Z for Windows to indicate EOF.

5
  • 1
    Control-C on Linux will (normally) interrupt the program so that the answer is not produced. You should normally type Control-D at the start of a line to indicate EOF. (The 'normally's are there because the keys are configurable.) Aug 29, 2015 at 2:16
  • @JonathanLeffler Yeah you're right I just thought wrong
    – JackV
    Aug 29, 2015 at 2:18
  • I'm on Mac OSX. Any idea why I have to press Ctrl+D twice? After pressing a+b+c+CtrlD, I get this: abc^D. When I press CtrlD another time, I get this: abc3D. Aug 29, 2015 at 3:29
  • @AdamZerner Im not entirely sure because i have never programmed in Mac OSX before and have barely even used it. What happens however if you type a b c press enter then press Ctrl+d and enter again? Does that work or do you still have to type Ctrl+d twice?
    – JackV
    Aug 30, 2015 at 19:41
  • That works, although it gives me output like 4D (the output I'm looking for is just 4). It seems like the D from ^D is remaining. Aug 30, 2015 at 20:15
1

Like everyone mention, you program doesn't stop. The while loop needs a condition to stop. you have to either use CTRL+Z (Win) or CTRL+D(Unix) to stop it, When you pipe it actually sends that character for you. Because EOF is END_OF_FILE. So when you hit the end of your file. the OS sends that character for you.

3
  • Picking nits: the Control-Z or Control-D is an indicator for EOF; the character does not get sent to the program. When you pipe the data, the EOF is indicated when all the data is read and no process can still write to the pipe; again, no character is sent to indicate EOF. In both cases, the read() system call (or one of its many relatives) returns 0 indicating that there is no more data. See Why is EOF recognized only if it is the first character on the line and other related questions. Aug 29, 2015 at 2:26
  • True. EOF is actually a condition. Meanwhile In the C Standard Library, the character reading functions such as getchar return a value equal to the symbolic value (macro) EOF to indicate that an end-of-file condition has occurred. The actual value of EOF is system-dependent (but is commonly -1, such as in glibc) and is unequal to any valid character code. Block-reading functions return the number of bytes read, and if this is fewer than asked for, then the end of file was reached. -Wiki
    – Fawzan
    Aug 29, 2015 at 2:29
  • Strictly, "if this is fewer than asked for", it means no more than the given data is available at the moment. For example, if a program asks for 1 KiB of data from a terminal but you only type Hi, then there will be 3 bytes read (H, i, and newline), but that doesn't indicate EOF. EOF is indicated when the return value is zero bytes read. Aug 29, 2015 at 2:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.