LearnFrenzy
Sign in Take a free test

Strings · String Basics MCQ · Question 9

Why cast to unsigned char before toupper()? Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

Why is toupper((unsigned char) c) recommended over toupper(c) for a char c?

  • A It is faster
  • B A plain char may be negative for bytes above 127, and passing a negative value (other than EOF) is undefined behaviour Correct
  • C toupper() only accepts unsigned types
  • D It changes the result for ASCII letters
Correct Answer: Option B

The ctype functions take an int that must be EOF or a value of unsigned char. Where char is signed, a byte such as 0xE9 (é in Latin-1) becomes −23 – undefined behaviour, and a real crash on some libraries.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular