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

Blair P. Houghton bhoughto at cmdnfs.intel.com
Wed Oct 17 07:05:28 AEST 1990


In article <2466 at ux.acs.umn.edu> edh at ux.acs.umn.edu (Eric D. Hendrickson) writes
>#include <ctype.h>
>main()
>{
>	char *duh = "Hello";
>	printf("%s\n", duh);
>	while (*duh <= strlen(duh)) {

Change `*duh' to `duh'.

>		if (islower(*duh)) *duh = toupper(*duh);
>		*duh++;

Ditto.  Increment the pointer, not the character.

>	}
>	printf("%s\n", duh);

Use a different variable here.  `duh' will now point to 'O', not 'H',
if the loop is entered.

>}
>
>And what I get is :
>Hello
>Hello

Basically, since `*duh' is a character, and a printable one at that,
its value as an integer in (*duh <= strlen(duh)) is going to
be something on the order of 60, while strlen(duh) is 5.  The
loop is skipped because 60 is never <= 5.

				--Blair
				  "End of lesson.  No opportunistic
				   comment on use of the word 'duh.'
				   ...except maybe indirectly..."



More information about the Comp.lang.c mailing list