Why do some versions of toupper act strangely if given an upper-case letter?

Q

Why do some versions of toupper act strangely if given an upper-case letter? Why does some code call islower before toupper?

✍: Guest

A

In earlier times, toupper was a function-like preprocessor macro and was defined to work only on lower-case letters; it misbehaved if applied to digits, punctuation, or letters which were already upper-case. Similarly, tolower worked only on upper-case letters. Therefore, old code (or code written for wide portability) tends to call islower before toupper, and isupper before tolower.
The C Standard, however, says that toupper and tolower must work correctly on all characters, i.e. characters which don't need changing are left alone.

2015-08-17, 1000👍, 0💬