Re: isspace etc and (int) typecast

From: Henrik Nordstrom <>
Date: Tue, 27 Mar 2007 21:18:01 +0200

tis 2007-03-27 klockan 13:16 +0200 skrev Guido Serassio:

> >What was the warnings you fixed with these casts? I suspect you only
> >silenced a valid warning here without fixing the cause..
> Exactly these:

Ok, what I thought..

the warning says that isspace(x) is implemented as a macro translating
into something like isspace_array[x]

with x being a char the compiler warns on array references as the
standard does not define if a char is signed or unsigned and thus the
results may differ from platform to platform.

What the warning really should have said is "watch out here, there is a
unqualified char type passed to a ctype function, you most likely want
to change or cast this into unsigned char".

if you cast into int the compiler doesn't warn on this as int is the
correct type and it's always valid to cast char into int, but the code
is still just as broken as before. If x is 0x80 the code becomes
isspace_array[-128] which is far outside the allowed range for isspace.
(assuming char signed by default, which is very common).

isspace is only valid on unsigned char values, and as a special case EOF
(usually -1). Any other use of ctype functions is outside
specifications. Luckily many implementations handle this correctly by
allowing ctype queries in the range -128 - +255 accounting both for
signed and unsigned char, but it can not be guaranteed.


Received on Tue Mar 27 2007 - 13:18:07 MDT

This archive was generated by hypermail pre-2.1.9 : Sun Apr 01 2007 - 12:00:01 MDT