C++ string literals: select the single option that is an <em>invalid</em> string constant (i.e., not a valid string literal) while others are valid strings.

Difficulty: Easy

Correct Answer: 'multi char'

Explanation:


Introduction / Context:
In C and C++, a string constant (string literal) is a sequence of characters enclosed in double quotes. It produces an array of characters terminated by a null character. This question asks you to identify which presented token is not a valid string literal, ensuring clarity about the difference between character and string constants.


Given Data / Assumptions:

  • Valid string literals use double quotes, for example, "text".
  • Character literals use single quotes and represent a single code unit (e.g., 'A').
  • Options include both quoted forms and content with digits and spaces.


Concept / Approach:

  • Anything in double quotes is a string literal, regardless of its textual content (digits, words, spaces).
  • Single quotes denote a character literal; more than one character between single quotes is not a string literal and is invalid as a character literal in strictly conforming code (multi-character literals are implementation-defined integer constants, not strings).
  • Therefore, an entry surrounded by single quotes containing multiple characters is not a valid string constant.


Step-by-Step Solution:

Inspect option A: "Hello, world!" → valid string literal.Option B: "7.15 pm" → valid string literal with digits, dot, and space.Option C: 'multi char' → uses single quotes and multiple characters → not a string literal; not a standard single character literal either.Option D: "i like e" → valid string literal.Option E: "1234e12" → still a string literal; numeric-like content does not change the type.


Verification / Alternative check:

Compile-time types: options A, B, D, E have type const char[N]; option C would be an implementation-defined multi-character literal of type int (not a string), or rejected under strict rules, hence invalid as a string constant.


Why Other Options Are Wrong:

  • "Hello, world!", "7.15 pm", "i like e", "1234e12": All are proper string literals because they use double quotes.


Common Pitfalls:

  • Confusing the lexical form (single vs double quotes) and assuming content determines type.
  • Misusing single quotes for words; only double quotes produce strings.


Final Answer:

'multi char'

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion