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 case, when calling getchar
, you are asking for something from the standard input that internally the function returns with one byte information. In the description of the function he even speaks of why an integer is the return:
The Return type is int to accommodate for the special value EOF, which indicates Failure
In free translation:
The type of return is int
to accommodate for the special value EOF
, indicating failure
Generally speaking, C works without much concern about the whole data types, converting from one to the other indiscriminately. When the conversion is from a type of smaller to larger size, it is said that the type has suffered a whole promotion.
The function putchar
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.
That translating gets:
The promotion int
of the character to be written.
The value is internally converted to a unsigned char
when written.
So, what happened here that worked in your code is that the character received by the standard entry underwent an entire promotion internally, before being returned by getchar
. Then you passed the promoted value forward to the function putchar
, that expects exactly a promoted value and then converts it to a no-signal character and prints it.
In the second paragraph, you said "standard input that internally returns the function with one byte information", which you meant by "one byte information"?
– user62320
From here,
signed char
: of the same size aschar
, an integer between[-128, 127]
;unsigned char
: of the same size aschar
, an integer between[0, 255]
. You only need one byte to store the information. In this case, the value 257 is theEOF
to indicate end of reading– Jefferson Quesado