The return value of getchar()
is of the same format as fgetc()
. C11 defines the return value of fgetc()
in 7.21.7.1p2-3:
- If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the
fgetc
function obtains that character as an unsigned char
converted to an int
and advances the associated file position indicator for the stream (if defined).
Returns
- If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end- of-file indicator for the stream is set and the
fgetc
function returns EOF
. Otherwise, the fgetc
function returns the next character from the input stream pointed to by stream. If a read error occurs, the error indicator for the stream is set and the fgetc
function returns EOF
. [289]
Since this is an unsigned char
converted to an int
, the int
will almost always have the same value as the unsigned char.
It might not be true for high values on some platforms where sizeof(int) == 1
; these however are mostly DSP platforms, so it is almost certain that character classification is not needed on these platforms.
The is*
functions are carefully defined so that they can be used directly with the return value of *getc*
C11 7.4p1:
1 The header <ctype.h>
declares several functions useful for classifying and mapping characters. [198] In all cases the argument is an int
, the value of which shall be representable as an unsigned char
or shall equal the value of the macro EOF
. If the argument has any other value, the behavior is undefined.
i.e. it is legal to even pass EOF
to the is*
functions. Of course isanything(EOF)
will always return 0, therefore to count continuous whitespace characters one could simply use something like:
while (isspace(getchar())) space_count ++;
However, signed char values are not OK, and for example MSVC C debug library is known to abort if a negative value other than EOF
is passed in to any of the character classification functions.