2

The following is a program to count the number of characters:

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

As we have seen here- Why does getchar() recognize EOF only in the beginning of a line? that ctrl+z is not considered as EOF when written within a line of characters and is considered as EOF only when written at the beginning of line.

So these are some of the following outputs of the program:

123
abs
^Z
8

Here the program returns 8 so it means it is counting the '\n' as well.

123^Z
abs^Z
^Z
8

It again returns 8 so what is the program doing here? Either it is ignoring ^Z as a character or it is not counting the '\n' after the ^Z.

abc^Zaa
^Z
4

Here the program is returning 4 so it means that it is not counting aa after ctrl+Z. So I want to know whether it is not counting any characters after ^Z or it is also not counting ^Z but is counting the newline character at the end of each line. So can it be said that ^Z here is also acting as sort of end of line?

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
RSSB
  • 144
  • 2
  • 4
  • 14
  • 2
    The question is more about the keyboard interface and driver than C: What keys it counts as a _character_. Keys Ctr Z becomes character `'\1A'` or signals an an "End-of-file" under different conditions. – chux - Reinstate Monica Mar 18 '18 at 17:45
  • Recommend printing the value returned by `getchar()` for greater insight. `int c; while((c = getchar()) != EOF) printf("%d %d\n", ++nc, c);` – chux - Reinstate Monica Mar 18 '18 at 17:48
  • 2
    Also, year 1999 was 19 years ago - you should write standard C, not some prehistoric variant - `int main(void)`. And @chux perhaps, `printf("%d %hhu", ++nc, (unsigned char)c)` – Antti Haapala -- Слава Україні Mar 18 '18 at 17:49
  • @chux But here it is also apparently not counting any characters after ctrl+z and also this is not the case with other key combinations such as ctrl+s or ctrl+g as it easily takes them as characters and count them along but some key combinations e.g., ctrl+f rather than being written as ^F perform certain action and are not even taken as input. – RSSB Mar 18 '18 at 17:51
  • 1
    Recall these are _control_ characters. There are meant to act differential then other characters as they control input. The precise functionality varies amongst computers. – chux - Reinstate Monica Mar 18 '18 at 17:55
  • `Ctrl-Z` is the end of file when it is read from the keyboard. When a real file is read from the disk, `'\1A'` (`Ctrl-Z`) is a character like any other. It doesn't signal anything. The file end is computed using the file length (the information is stored by the file system). `Ctrl-Z` is treated as the end of input because there is no way to know in advance the length of the input when it is read from the keyboard. – axiac Mar 18 '18 at 18:34
  • 1
    Possible duplicate of [Why does getchar() recognize EOF only in the beginning of a line?](https://stackoverflow.com/questions/14436596/why-does-getchar-recognize-eof-only-in-the-beginning-of-a-line) – Jean-Baptiste Yunès Mar 19 '18 at 07:02

1 Answers1

2

It may depend on your OS, but in general at the beginning of the line it means close the input stream and in the middle of a line flush input stream. So in any case your control char is available on input (except if you modify the terminal behavior). Try this and you will see:

int c;
while((c=getchar()) != EOF) {
    ++nc;
    printf("read ascii code %d\n",c);
}
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • But what does it mean to flush input stream – RSSB Mar 18 '18 at 18:05
  • That's another problem, but shortly input chars are not available at the time you input them, only when the terminal input buffer is flushed (when it is full, when you enter and when you enforce flush). – Jean-Baptiste Yunès Mar 18 '18 at 18:08