ASCII – Difference Between EOT and EOF

ascii

I understand that EOT is ASCII code 4, whereas EOF is encoded as -1 (at least in C). Before I found out that EOF is mapped to -1, I thought it was just a synonym for EOT. Why is EOF mapped to -1 rather than EOT? As far as I can tell, they both do the same thing, which is to terminate a file stream. The only difference I can discern is that EOT also terminates a command in the bash shell. I would like a description of the precise technical differences between these two codes.

Best Answer

Generally, EOF isn't a character; it's the absence of a character.

If a program runs on a terminal in canonical mode with default settings (i.e. a plain C program that just uses stdio), it will never see the ASCII character EOT. The terminal driver recognizes that character and creates an EOF condition (which at the low level is a 0 return value from read()). The stdio library translates that EOF condition into the return value that is appropriate for the function in question (the EOF macro for getchar(), a null pointer for fgets(), etc.)

The numeric value of the EOF macro is of no relevance anywhere but in the C library, and it shouldn't influence your understanding of the meaning of the EOF condition.

Related Question