LearnFrenzy
Sign in Take a free test

C Const · const MCQ · Question 8

What is the type of a string literal in C? Answer, explanation & discussion

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

What type does the string literal "hi" have in C?

  • A const char[3]
  • B char[3] – not const, but modifying it is still undefined behaviour Correct
  • C char *
  • D const char *
Correct Answer: Option B

For historical reasons, string literals are char arrays in C (they are const char arrays in C++). So char *s = "hi"; compiles without a warning, but writing through s is undefined. Declare const char *s.

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