user31782
user31782

Reputation: 7589

Understanding getchar() in the character counting program in C

This is a follow-up question of my previous question. There is already a similar question asked(question). But I don't get what I want to know from that answer.

From the previous question I come to know that if I type a lot of characters, then they are not made available to getchar(), until I press Enter. So at the very point when I press Enter, all the characters will be made available to getchar()s. Now consider the following program for character counting:

#include<stdio.h>
main()
{
  long nc;
  nc=0;
  while(getchar()!=EOF)
  ++nc;
  printf("    Number of chars are %ld ",nc);
}

If I input characters from the command line in the following sequence: {1,2,3,^Z,4,5,Enter}, then in the next line {^Z,Enter}. The output that I expect is: Number of chars are 6. But the output that I am getting is Number of chars are 4.

This answer explains that when we input1,2,3,^Z, then ^Z acts like Enter and 1,2,3 are sent to getchar()s. The while loop of the above written code runs three times. ^Z is not given to getchar(), so the program doesn't terminate yet. My input was {1,2,3,^Z,4,5,Enter}. After ^Z I had pressed 4,5 and then Enter. Now when I press Enter the characters 4,5 and Enter, should be given to getchar()s and the while loop should execute three times more. Then in the last line I input {^Z,Enter}, since there is no text behind ^Z, it is consider as a character and when I press Enter, this ^Z is given as the input to getchar() and the while loop terminates. In all this, the while loop has executed 6 times, so the variable nc should become 6.

  • Why am I getting 4 as the value of nc, rather than 6.

Upvotes: 3

Views: 1752

Answers (2)

user539810
user539810

Reputation:

Adding some output will help you:

#include <stdio.h>
int
main (void)
{
  int c, nc = 0;
  while ((c = getchar ()) != EOF)
    {
      ++nc;
      printf ("Character read: %02x\n", (unsigned) c);
    }
  printf ("Number of chars: %d\n", nc);
}

The Windows console views the ^Z input as "send input before ^Z to stdin, discard remaining input on the line (including the end-of-line delimiter), and send ^Z" unless it is at the beginning of a line, in which case it sends EOF instead of ^Z:

123^Z45
Character read: 31
Character read: 32
Character read: 33
Character read: 1a
^Z12345
Number of chars: 4

Also, Windows always waits for the Enter/Return key, with the exception of very few key sequences like ^C or ^{Break}.

Upvotes: 3

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

^Z, or Ctrl-Z, means end-of file for text files (old MS-DOS). getchar() is equivalent to fgetc(stdin) and is often a macro. "fgetc returns the character read as an int or returns EOF to indicate an error or end of file."

See also _set_fmode, however, I am not sure if that changes the behaviour right away or whether you have to close/reopen the file. Not sure either if you can close/reopen stdin (don't do much console programming anymore).

Upvotes: 1

Related Questions