LearnFrenzy
Sign in Take a free test

C Library Functions · Find the Error · Question 6

Error: toupper() applied to a whole string Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <ctype.h>
#include <stdio.h>

int main(void) {
    char word[] = "hello";
    printf("%c\n", toupper(word));
    return 0;
}
  • A toupper needs stdlib.h
  • B toupper converts one character (an int); passing the array (a char pointer) is invalid – loop over the characters Correct
  • C %c should be %s
  • D There is no bug
Correct Answer: Option B

Convert each character: for (size_t i = 0; word[i]; i++) word[i] = (char) toupper((unsigned char) word[i]);.

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.

Search LearnFrenzy

Esc

Quick links