The C17 (N2176) standard states, "The fputc function returns the character written" (7.21.7.3) if there is no write error.
But in the context of
int c ;
// ... later, c is assigned an "interesting" value ...
int k = fputc ( c , stdout ) ;
is
k == c || k == EOF
always true
? I.e., provided that fputc
does not return EOF
, is it guaranteed to return c
? Put a third way, can fputc
write a character other than one equal to its first argument?
For example If I request output of the dollar sign (not guaranteed to be in the source or execution character sets, AFAICT), could '\u0024' != fputc('\u0024', stdout)
. Maybe the program will output a local currency symbol, instead.
CodePudding user response:
is
k == c || k == EOF
always true?
Usualy yes, but no.
With fputc ( c , stdout )
, c
is converted to an unsigned char
and that is written. It is that value, or EOF
that is returned.
Instead, expect k == (unsigned char) c || k == EOF
.
CodePudding user response:
Yes, fputc
always returns either EOF
or the written character.
Per cppreference.com's page on fputc
:
Return value
On success, returns the written character.
On failure, returnsEOF
and sets the error indicator (seeferror()
) on stream.
And from the C17 standard (7.21.7.3/3 "The fputc function", N2310):
Returns
Thefputc
function returns the character written. If a write error occurs, the error indicator for the stream is set andfputc
returnsEOF
.