Is there a good example of how toupper() works?

Brad Appleton brad at SSD.CSD.HARRIS.COM
Thu Oct 18 23:36:00 AEST 1990


In article <wb76pN600awOE3SaQz at andrew.cmu.edu> jh4o+ at andrew.cmu.edu (Jeffrey T. Hutzelman) writes:
>Try this one:
>
>void strupper(char *str)
>{
>for (;*str!='\0';str++)
>	*str=toupper(*str);
>}


You need to be careful here! It all depends on your compiler. For some
compilers, the toupper function/macro performs the functional equivalent
of:

	c = (c - 'a') + 'A';

with other compilers, the functionality of toupper is more like this:

	if ( c >= 'a'  &&  c <= 'z' )
		c = (c - 'a') + 'A';


In other words, some compilers will blindly convert the character to uppercase,
regardless of what the character was whereas other compilers will make sure
the value is indeed lowercase before trying to modify it to be uppercase.

You will have to double-check your documentation for this. I think that 
the BSD Unix/C toupper() MUST take a lowercase letter and has undefined
results otherwise whereas the AT&T Unix/C toupper() will give the desired
result even if the character was not lowercase to begin with (Im not 100%
positive about that though, anyone care to enlighten me).

______________________ "And miles to go before I sleep." ______________________
 Brad Appleton        brad at travis.ssd.csd.harris.com   Harris Computer Systems
                          ...!uunet!hcx1!brad          Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~



More information about the Comp.lang.c mailing list