In c , a variable of type char
is an integer of only a single byte. Whether it will be interpreted graphically or numerically will depend on the context.
In this case, when you call getchar
, you are asking for something from the default input that internally the function returns with one-byte information. In the description of the function it even speaks of why an integer is the return:
EOF, which indicates failure
Free translation:
The return type is int
to accommodate for the special value EOF
, which indicates failure
In general, C works without much concern about integer data types, converting from one to the other indiscriminately. When the conversion is from a type of smaller size to a larger size, the type is said to have undergone an integer promotion .
The putchar
function says the following about your argument:
The int promotion of the character to be written.
The value is internally converted to an unsigned char when written.
Translating stays:
The promotion int
of the character to be written.
The value is internally converted to unsigned char
when typed.
So what happened here that worked right in your code is that the character received by the standard input has undergone an entire promotion internally before being returned by
getchar
. So you passed the promoted value forward to the
putchar
function, which expects exactly one promoted value, and then converts it to an unsigned character and prints it.