C/C++ literals: which of the following characters are valid inside a numeric literal token (as part of the literal itself) in standard C++?

Difficulty: Easy

Correct Answer: None of the above

Explanation:


Introduction / Context:
Numeric literals in C and C++ follow strict lexical rules. Only certain characters are permitted within the token that represents an integer or floating-point literal. Understanding which characters are allowed prevents hard-to-spot compile errors and ensures code portability across compilers and locales.


Given Data / Assumptions:

  • We refer to the characters inside a numeric literal, not separators in source formatting.
  • Standard C++ permits digits, decimal points, exponent markers (e/E, p/P for hex floats), sign characters in exponents, base prefixes (0x, 0b in C++14/20 compilers), digit separators ('') and type suffixes (u, l, f, etc.).
  • We evaluate specific candidates: comma, dollar sign, percent sign, and space.


Concept / Approach:

  • Commas and spaces are token separators in the grammar, not part of numeric literals.
  • Dollar ($) and percent (%) are ordinary punctuation/operators; they are not valid within numeric literals.
  • Therefore, none of the listed characters are valid inside a numeric literal token.


Step-by-Step Solution:

Check each character against the lexical set for numeric literals.Comma → not allowed; Space → not allowed; $ and % → not allowed.Conclude: the correct option is ”None of the above”.


Verification / Alternative check:

Try compiling 1,000 or 1 000 or $100 or 50% as literals: each fails or parses differently than intended. Use 1000, 1'000 (C++14 digit separator), or 0.5 for 50% semantics.


Why Other Options Are Wrong:

  • a comma / a space: Break tokens into multiple tokens.
  • a dollar sign ($) / a percent sign (%): Not part of literal syntax; % is the modulo operator.


Common Pitfalls:

  • Copying numbers from documents with thousands separators; remove commas or use the digit separator ''.
  • Confusing formatting symbols (%, $) with literal syntax; those belong in strings or comments.


Final Answer:

None of the above

Discussion & Comments

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