Q17
Which of the following is not a valid C++ identifier?
A.
_variableName
B.
2ndVariable
C.
variable_name@
D.
VariableName
E.
Both (B) and (C)
AnswerAnswer: Option E
Solution
Answer: Option E
Solution:
C++ Identifiers: Naming RulesIn C++, identifiers are names you give to things like variables, functions, and classes. They must follow specific rules.
Rules for Valid Identifiers:
1. They can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
2. They must begin with a letter or an underscore.
3. They cannot contain spaces.
4. They cannot be C++ keywords (like
int, float, while, etc.).Let's Check the Options:
A)
_variableName: This is valid. It starts with an underscore and uses letters.B)
2ndVariable: This is invalid. Identifiers cannot start with a digit.C)
variable_name@: This is invalid. It contains the "@" symbol, which is not allowed.D)
VariableName: This is valid. It starts with a letter and uses letters.Therefore, the correct answer is E, Both (B) and (C) are not valid C++ identifiers.