proposed changes to eliminate P macro
eggert at CS.UCLA.EDU
Thu Nov 15 20:08:31 UTC 2007
Robert Elz <kre at munnari.oz.au> writes:
> if we have char *cp; and want to use an unsigned char (particularly
> for args to the isalpha() etc macros), I'd feel safer writing
> *(unsigned char *)cp
> rather than the
> (unsigned char) *cp
> that now exists. I just don't trust compilers to always evaluate the
> latter correctly.
I understand that lack of trust, and it was a deserved lack of trust
20 years ago or maybe even 15. But nowadays we can trust compilers to
do the latter version accurately. Conversely, the former version is
not portable code according to the C standard: it allows
implementations where 'char' has weird representations that don't work
well when interpreted as 'unsigned char'. In such implementations,
the latter code is reliable (its semantics are defined by the C
standard, and work in the usual way) whereas the former is not.
As a personal preference, I now avoid all casts in C, as I think their
disadvantages now almost always outweigh their advantages. Instead of
the above, I'll write code like this:
unsigned char uc = *cp;
... uc ...
This works on all C compilers nowadays.
More information about the tz