use of toupper (was: Style (was: C-DATABASE B-PLUS a quick look))
Karl Heuer
karl at haddock.ima.isc.com
Thu Jan 5 10:54:00 AEST 1989
In article <2537 at xyzzy.UUCP> throopw at xyzzy.UUCP (Wayne A. Throop) writes:
> if( *p >= 0 ) *p = toupper( *p );
Wrong, because this would fail to translate a lowercase character whose value
appears negative. (In a non-ASCII environment.)
In article <2581 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
> if(islower(*p)) *p = toupper(*p);
Wrong, because if *p appears negative, it's not valid as input to islower().
In article <189 at becker.UUCP> bdb at becker.UUCP (Bruce Becker) writes:
> *p = toupper( *p&0xff );
Wrong, because bytes need not be eight bits wide. But this one is closest; it
can be made correct by changing it to
*p = toupper((unsigned char)*p)
or by declaring p to be of type `unsigned char *' in the first place (which
may have other ramifications).
>Not all _ctype arrays have the same range - some are only 128 bytes. In those
>cases the '0xff' above becomes '0x7f'.
No, in 128-element ctype implementations (which are necessarily pre-ANSI),
it's generally more correct to use
if (isascii(*p)) *p = toupper(*p);
or (if toupper() has the more restricted domain)
if (isascii(*p) && islower(*p)) *p = toupper(*p);
Silently ignoring the high bit is only correct in special cases, in programs
that use it for quoting and such.
In article <9256 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <189 at becker.UUCP> bdb at becker.UUCP (Bruce Becker) writes:
>>I'm confused about what the value of "toupper(EOF)" should be...
>
>I don't think EOF is supposed to be a valid argument for toupper(),
>just for the is*() functions.
I don't see any exception being made in the dpANS, so by my reading, EOF is in
the domain of toupper().% If this is correct, the proper return value is EOF.
(Just as toupper('#') must return '#'.)
Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
________
% This is probably about as useful as having it in the domain of the is*()
functions. In both cases, I can imagine a use for it, but in my own code, I
generally check for EOF before doing *anything* else with a getc() result.
More information about the Comp.lang.c
mailing list