Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding getchar() in the character counting program in C

Tags:

c

io

getchar

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.
like image 745
user31782 Avatar asked Dec 02 '25 14:12

user31782


1 Answers

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}.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!