Understanding Identifiers in C++
Identifiers are names given to variables, functions, classes, etc., to identify them uniquely in the code. They must follow certain rules in C++.
Key Topics
Rules for Identifiers
- Must begin with a letter (a-z, A-Z) or underscore (_)
- Can contain letters, digits (0-9), and underscores
- Case-sensitive (
Rudra
andrudra
are different) - Cannot be a reserved keyword (e.g.,
int
,class
)
Examples of Valid and Invalid Identifiers
Valid Identifiers
int Tamilmaran;
float salary_2023;
char _initial;
Invalid Identifiers
int 2ndPlace; // Starts with a digit
float double; // 'double' is a reserved keyword
char first-name; // Hyphen is not allowed
Code Explanation: Valid identifiers follow the naming rules, while invalid ones violate them by starting with digits, using reserved keywords, or containing illegal characters.
Key Takeaways:
- Choose meaningful identifiers to enhance code readability.
- Adhere to naming conventions and rules.
- Avoid using reserved keywords as identifiers.