Search code examples
c++toupper

why does these toupper work different when utilized like this?


why does one give me an int and the other doesn't?:

    toupper(member_names[2]);

and:

    member_names[2] = toupper(member_names[2]);

Solution

  • toupper takes a character (encoded into an int for mostly-historical reasons) and returns the upper-case equivalent of that character.

    Therefore, your first version doesn't really accomplish anything. Your second converts member_names[2] to its upper-case equivalent.

    Also note that (in most implementations) a char can have a negative value (e.g., accented characters in ISO 8859-*). Passing a negative value to toupper can/will lead to (serious) problems--unless member_names is an array of unsigned char, you normally want to case to unsigned char before passing to toupper:

    member_names[2] = toupper(static_cast<unsigned char>(member_names[2]));