I am trying to learn C on my own and I\'m kind of confused with getchar
and putchar
:
#include
int main(v
Always use int
to save character from getchar()
as EOF
constant is of int
type. If you use char
then the comparison against EOF
is not correct.
You can safely pass char
to putchar()
though as it will be promoted to int
automatically.
Note:
Technically using char
will work in most cases, but then you can't have 0xFF character as they will be interpreted as EOF
due to type conversion. To cover all cases always use int
. As @Ilja put it -- int
is needed to represent all 256 possible character values and the EOF
, which is 257 possible values in total, which cannot be stored in char
type.