-
putc() != fputc() ?
I'd like to ask what's the difference between library functions
putc() and fputc(). Their functional explanation in my C
reference is EXACTLY the same. The only difference is this
sentence in fputc(), "Even though ch is declared to be an int
for historical reasons, it is converted by fputc() to an
unsigned char."
So,
1) Aren't the two functions supposed to be exactly the
same and perform exactly the same operations ?
2) When fputc()'s ch argument
is converted to an unsigned
char since it is declared as an int in its prototype because
character arguments are elevated to ints at the time of the call ?
int fputc( int ch, FILE *stream );
3) The same facts apply in another the case of lib functions
fgetc() and getc(). Any comments on this, too ?
thanks,
trekker
-
They are identical, but both exist "to preserve compatibilitywith older versions of C" according to Herbert Schildts 'C The complete reference'.
Mind you, this book is widely regarded as being next to useless by many contributors to this board!
foffo
-
fputc is the primitive function for which the behavior of other input functions is based off of. Whether or not fputc is actually used in these functions is irrelevant. fputc is usually a function but can be masked with a macro.
putc was traditionally a macro mask for fputc, but it is also required to be implemented as a true function. The difference between putc and fputc is that by using putc, you risk running the macro version which is inherently unsafe because it may have to evaluate its stream argument more than once. This causes complications that most people aren't aware of and thus do not watch out for, so fputc is better to use. fputc's macro does not have this problem.
-Prelude