In C, strings are arrays of char (char *
) and characters are usually stored in char
. I noticed that some functions from the libC are taking as argument integers instead of a char.
For instance, let's take the functions toupper()
and tolower()
that both use int
. The man page says:
If c is not an unsigned char value, or EOF, the behavior of these functions is undefined.
My guess is that with a int
, toupper
and tolower
are able to deal with unsigned char
and EOF
. But in fact EOF
is in practice (is there any rule about its value?) a value that can be stored with a char
, and since those functions won't transform EOF
into something else, I'm wondering why toupper
does not simply take a char as argument.
In any case why do we need to accept something that is not a character (such as EOF)? Could someone provide me a relevant use case?
This is similar with fputc
or putchar
, that also take a int
that is converted into an unsigned char
anyway.
I am looking for the precise motivations for that choice. I want to be convinced, I don't want to answer that I don't know if someone ask me one day.
EOF
must fit into achar
, and I can assure you thatchar
is not guaranteed to be signed, which makes your discussion about usingchar
instead ofunsigned char
seem the wrong one. You meansigned char
throughout. – Optint
parameter is for optimization purposes, because its byte size well fit to sizes of the processor registers. In turn, one bytechar
variable must be converted toint
behind the curtain, and this operation need some processor time to process. – Kentonkentucky