Search code examples
ceofgetcharputchar

I am trying to under eof and getchar ()


In the piece of code given below. Whenever I am inputing a set of characters with a ctrl+z at the end, which should mark EOF for getchar(). It is printing all the characters along with another character at the end which has an ASCII value of 26. I dont know the character by name.

I expect the program to end wherever I put a ctrl+z. However Ctrl+z is only working when the input stream is empty.

Can someone explain why is it returning such an awkard character instead of ending the program ?

#include<stdio.h>
#include<ctype.h>

int main(){

   int c;

   while((c = getchar()) != EOF)
       putchar(c);

   return 0;
}

Sorry I couldn't upload the image, neither do I have any way of showing the character which has that value of 26. ASCII table charts say 26 is /substitute/. I don't know what that means. Thank You.


Solution

  • Ctrl+Z is an ASCII control character like Ctrl+A, Ctrl+B, Ctrl+@ and the others. It is not EOF. EOF is distinct from any character: otherwise, if getchar() returned that value, you couldn't know whether it had reached the end of the file or it had read that character. Ctrl+Z has the ASCII value 26. You're seeing what you typed.

    On your operating system (Windows, presumably), Ctrl+Z at the beginning of a line is an indication to the terminal that you've finished typing. (More precisely, I think the code is in the library functions that read from the terminal, and not from the terminal itself. But I may be wrong on the details as I'm not a Windows expert.) It only works at the beginning of a line because that's how the library code that reads from the terminal works. When you type Ctrl+Z at the beginning of a line, getchar() sees an end-of-file condition and returns EOF.

    When you type Ctrl+Z not at the beginning of a line, it's treated as an ordinary character. Windows prints it out as a “strange glyph” that it uses to indicate an unprintable character.

    The official meaning of most ASCII characters was forgotten a long time ago. There is an ASCII control character that used to mean “end of file”, or more precisely “end of transmission”, which is Ctrl+D, and it survives in the Unix operating system family to mean “end of input” when entered at the beginning of a line on a terminal, much like Ctrl+Z on Windows. The association of Ctrl+Z with end-of-file on Windows comes from a different historical line of operating systems, mostly from DEC, that Microsoft's DOS and Windows took inspiration from.

    Not exactly the same question as yours, but I recommend this answer which should help clear your confusion about EOF.