tags:

views:

41

answers:

1

standard C lib:

int fputc(int c , FILE *stream);

And such behaviors occured many times, e.g:

    int putc(int c, FILE *stream);
    int putchar(int c);

why not use CHAR as it really is? If use INT is necessary, when should I use INT instead of CHAR?

+5  A: 

I believe that it was simply to mirror the fgetc type functions which must be able to return any real character plus the EOF special character.

To do that, they needed an int type since a char isn't quite large enough.

paxdiablo
Your belief is right! :)
LukeN
But we won't write EOF to an FILE whenever...So you said 'simply mirror' ,am I right?^_^
HaoCheng
So what is the behaviour of `putchar(-1)` ?
anon
No, you don't write EOF but, in a simple stdin/stdout filter program, it's easier just to use the one (int) variable rather than to try and coerce types.
paxdiablo
@Neil, the standard says it's converted to an unsigned char.
paxdiablo
Get it^_^ further more, must I use INT instead of CHAR to be an function parameter as a trick from now on?
HaoCheng