char array subscript in ctype

Peter Naulls peter at
Mon Dec 31 03:11:49 PST 2001

In message <20011230235037.GA3181 at>
          Vincent Lefevre <vincent at> wrote:

> On Sun, Dec 30, 2001 at 18:34:26 +0000, Peter Naulls wrote:
> > I'm not sure that's a valid argument.  isspace and friends are almost
> > always used in the context of unqualified chars (unsigned on ARM, signed
> > on x86).   Futhermore, the specification for these functions says that
> > the integer passed is interpreted as an unsigned char (or EOF).  The
> > cast to unsigned int ensures this.
> No, if you cast the argument to unsigned int in the macro definition,
> EOF will become positive, which is incorrect. In __ctype[c], the EOF
> index is (in this implementation) -1. With the cast, the wrong byte
> will be read.

This is a genuine concern, however having checked this, the code
produced for -1 vs 2^32-1U is the same.

> > That aside, the real point is that the library headers cause a warning,
> > which they really shouldn't.
> They should, as using a char may be an error. For instance, in Mutt,
> there was such an error when accented characters (signed under some
> implementations) were used. The bug was in Mutt, not in the compiler's
> headers. Since then, casts have been added in Mutt.

That's unfortunate.  However (and very much at the risk of restarting
a debate from elsewhere) if the C libraries (rightly or wrongly) had had
a cast such as I suggest, chars < 0 would correctly be interpreted in
the right range when cast to unsigned.  

While no doubt warnings are very useful, I fear that the one produced in
this case is somewhat confusing, especially to a novice programmer
(lclint might throw up something more useful); I spent quite sometime
working out what exactly was generating the warning.

Having said that, if the consensus is that it's still a bad idea, then
that's fine.  Although I would suggest some commentary on the matter


 Peter Naulls - peter at
 RISC OS Projects Initiative  -
 Java for RISC OS and ARM     -
 Debian Linux on RiscPCs      -

More information about the gcc mailing list