C identifier rules: Which special symbol is allowed within a variable name according to standard C/C++ naming conventions?

Difficulty: Easy

Correct Answer: _ (underscore)

Explanation:


Introduction / Context:
Valid identifiers are essential for readable, portable code. C and C++ restrict variable names to a specific character set so compilers can parse tokens unambiguously. This question checks your knowledge of which special symbol is permitted within a variable name without invoking operator parsing or undefined behavior.


Given Data / Assumptions:

  • Identifiers typically consist of letters (A–Z, a–z), digits (0–9), and possibly one special character.
  • The first character of an identifier cannot be a digit.
  • We are concerned with ASCII-like basic source character set; extended identifiers exist but are not the focus.


Concept / Approach:

  • The only commonly allowed special character in standard C/C++ identifiers is the underscore ''.
  • Other symbols like '', '|', and '-' are operators or punctuation and cannot appear in identifiers.
  • Although identifiers beginning with underscores may be reserved in certain contexts, underscore itself is permitted.


Step-by-Step Solution:

Eliminate operator characters: '' (multiplication), '|' (bitwise OR/pipe), '-' (minus/negation).Confirm the remaining candidate '' as valid within identifiers.Hence, the allowed special symbol is the underscore.


Verification / Alternative check:

Try compiling variables named data_value, value2, and a-b; only the hyphenated name fails because '-' is parsed as an operator.


Why Other Options Are Wrong:

  • *, |, -: These are operators with fixed meanings in expressions and cannot appear in identifiers.
  • None of the above: Incorrect because underscore is valid.


Common Pitfalls:

  • Using identifiers beginning with an underscore plus a capital letter or double underscore; these are reserved for the implementation.
  • Starting identifiers with digits; that is invalid syntax.


Final Answer:

_ (underscore)

More Questions from Declarations and Initializations

Discussion & Comments

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