tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: using the interfaces in ctype.h

At 08:47 AM 4/17/2008 -0400, Greg A. Woods; Planix, Inc. wrote:

However what you and Alan said about simply casting to (unsigned char)
isn't sufficient, at least on some platforms (IRIX-6, IIRC), at least
not to keep the compiler happy.

Sadly I do get the least warnings from every platform when the cast is
only to (int).  I wonder if the compiler can be taught to detect uses
where a parameter may have a value out of range even though the data
type it is declared as is wide enough.  The only solution the compiler
can really believe though, I think, would be a double cast where the
value is narrowed sufficiently before being widened again for the call.

Hmm. Back in the bad old days before C-89, casts didn't portably guarantee that the result of casting was any different than what you started with. So for example isspace((uchar) x) could be wrong if x was > 0xFF.

I remember that the recommended practice (when using Whitesmiths C) was instead:

        isspace(x & 255)

I think thks probably still works -- a smart compiler should be able to do the modulo 256 reduction without actually masking, on most platforms. In any event, when using modern compilers, the '&' will make everything an int, and shouldn't cause any warnings. (Of course, the usual #defines and so forth would normally be used instead of writing '255'....) Also, it avoids a cast, which normally is better.

Best regards,

Home | Main Index | Thread Index | Old Index