Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why fputc function has integer as argument instead of unsigned char? [duplicate]

Tags:

function

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?

like image 406
HaoCheng Avatar asked Dec 06 '25 15:12

HaoCheng


1 Answers

Most likely (in my opinion, since much of the rationale behind early C is lost in the depths of time), it it was simply to mirror the types used in the fgetc type functions which must be able to return any real character plus the EOF special character. The fgetc function gets the next character converted to an int, and uses a special marker value EOF to indicate the end of the stream.

To do that, they needed the wider int type since a char isn't quite large enough to hold all possible characters plus one more thing.

And, since the developers of C seemed to prefer a rather minimalist approach to code, it makes sense that they would use the same type, to allow for code such as:

filecopy(ifp, ofp)
    FILE *ifp;
    FILE *ofp;
{
    int c;
    while ((c = fgetc (ifp)) != EOF)
        fputc (c, ofp);
}
like image 153
paxdiablo Avatar answered Dec 09 '25 23:12

paxdiablo