Notes – Identifier & Keywords in C++
In C++, identifiers and keywords are basic elements of any program.
- Identifiers are the names used to identify variables, functions, classes, arrays, etc.
- Keywords are reserved words in the language that have special meaning and cannot be used as identifiers.
What Is an Identifier?
An identifier is the name given by the programmer to a variable, function, class, or object.
Examples:
int age;
float totalMarks;
void printData() {}
In the above code:
age,totalMarks, andprintDataare identifiers
Rules for Naming Identifiers
| Rule | Description |
|---|---|
Must begin with a letter (AโZ, aโz) or underscore _ | |
| Can include letters, digits (0โ9), and underscores | |
| Cannot start with a digit | |
| Cannot use C++ keywords as identifiers | |
Case-sensitive โ Score and score are different | |
Avoid special characters like @, $, % (not allowed) |
Valid vs Invalid Identifiers
| Valid Identifiers | Invalid Identifiers | Reason |
|---|---|---|
totalMarks | 2ndPlace | Cannot start with a digit |
employee_name | float | float is a keyword |
_value123 | student-score | Hyphen not allowed |
Tips for Writing Good Identifiers
- Use meaningful names (
totalAmountinstead ofta) - Use consistent naming styles like
camelCaseorsnake_case - Avoid single-letter names (unless in loops like
i,j)
What Are Keywords?
Keywords are predefined and reserved words in C++.
They have special meanings and cannot be used as variable or function names.
Common C++ Keywords
int float return class public
private if else while for
break continue void this new
delete try catch throw const
C++ has over 60 keywords. You canโt use any of them as identifiers.
