Interview Questions

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

C Interview Questions and Answers


(Continued from previous question...)

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?

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.

(Continued on next question...)

Other Interview Questions