I attached a picture of a piece of c code that I don't completely understand. I know that getchar
returns the next character from standard output and that putchar
puts the character on the standard output, but i don't fully understand what the EOF
is and what this code is exactly doing. Also i don't get how you can add or subtract letters as it does in the if statement.

- 28,186
- 12
- 57
- 75

- 3
- 3
-
2You should include the code in the question and not have users have to navigate to another site. – David Lee Oct 16 '17 at 21:47
4 Answers
EOF means End Of File. This is used when you read from/to a file to detect if a file has ended and there is no more characters to read. You can add/subtract char datatype values as it is basically a 8 bit memory location and char is usually stored as an ASCII. Google for ASCII and understand how numbers are mapped to characters, then you would understand how subtraction can be done on characters.
Basically the code written there inputs characters from standard input, and if the value is between A and Z, converts them to lower case and prints them back.
And by the way, that is no pseudo code. The photo you attached there is actual working C code.

- 90
- 1
- 7
some clarification: EOF = end of file. Every file has this character at its end to signal that the file is over
in c chars are represented as int from the ascii table (http://www.asciitable.com/). So writing 'A' is the same as writing 65. C does not make a difference between the two.

- 641
- 5
- 15
This reads characters from a file (until it reaches the EOF character indicating "End of File"). For characters that are uppercase (between 'A' and 'Z') it converts them to lowercase then prints that character, otherwise it just prints the original character.
To understand the character arithmetic, you might want to look at an ascii table that shows the numeric values typically assigned to characters.
(I say "typically" out of excessive caution since you said this is pseudocode, but it looks like perfectly valid C to me.)

- 3,506
- 1
- 9
- 9
-
-
@LeeDanielCrocker Not sure what I'm missing. You mean because getchar() returns an int? I still think this would compile and work, since the man page says "Character values are returned as an unsigned char converted to an int" so the int returned will always fit in a char. – EdmCoff Oct 17 '17 at 14:44
-
1No, the value EOF is specifically designed to be different from any possible char. If you squeeze the return of getchar() into a char, then EOF will be chopped down to whichever char has the same lower 8 bits as the EOF value. If you don't need that char, and you treat it as EOF, then your code will work--you just won't be able to get that char. – Lee Daniel Crocker Oct 17 '17 at 17:55
-
First, the code won't work as written until you change the declaration of c
to int
. The function getchar()
returns an int
, not a char
. Most of the time, that int contains only an 8-bit value identical to the character just fetched from input. When there's no more input, it contains the special integer value EOF
, which is distinct from any character.
This code assumes that the integer values of A to Z and a to z are consecutive. That's true for ASCII and other codes, but not universal. If it is true, then subtracting 'A' from an uppercase character will produce an integer from 0 to 25, then adding 'a' to that will produce the corresponding lowercase character.

- 12,927
- 3
- 29
- 55