tech-userlevel archive

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

Re: using the interfaces in ctype.h



On Apr 16, 10:24pm, woods%planix.ca@localhost ("Greg A. Woods; Planix, Inc.") 
wrote:
-- Subject: Re: using the interfaces in ctype.h

| 
| On 16-Apr-08, at 6:30 PM, Christos Zoulas wrote:
| > - cast ctype arguments to unsigned char and not int, otherwise you  
| > silence
| > the warning but you can still get a core-dump.
| 
| 
| If you're talking about the is*() and to*() APIs from libc then that  
| doesn't seem to make any sense for a standards compatible program.
| 
| The definitions in all of the standards, an in the NetBSD manual  
| pages, all request that the parameters for these functions are to be  
| passed as (int).
| 
| Unfortunately even various NetBSD architectures, at least for some  
| releases, are inconsistent in how these interfaces are defined and  
| what warnings are given for various usages.  I've had the best luck  
| getting the least warnings on a wide variety of platforms by always  
| casting to (int) too.

It is not an issue of avoiding the warnings. For example isspace() is
defined in:

    http://www.opengroup.org/onlinepubs/009695399/functions/isspace.html

as:

    The c argument is an int, the value of which the application
    shall ensure is a character representable as an unsigned char
    or equal to the value of the macro EOF. If the argument has
    any other value, the behavior is undefined.

So by casting a signed char to an int, you can get a large negative number
and you end up with "undefined behavior" which might mean a core dump,
so don't do it!

christos


Home | Main Index | Thread Index | Old Index