LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Find the Error · Question 9

Error: C array with too many initializers Answer, explanation & discussion

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

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    char code[4] = "PUNE1";
    std::cout << code << '\n';
    return 0;
}
  • A char arrays cannot hold letters
  • B "PUNE1" needs 6 chars including the terminating '\0', but the array has only 4 Correct
  • C cout cannot print char arrays
  • D Strings need single quotes
Correct Answer: Option B

A string literal includes its null terminator. Write char code[] = "PUNE1"; (size 6) or use std::string.

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