What are Identifiers ?
Identifiers in C++ can be composed of letters, digits, and the underscore character.
Identifier is the sequence of characters that make up the name.
Ex:
1 2 | int wrap; float _pro; |
Rules of Identifiers
- An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_). Special characters and spaces are not allowed.
- An identifier can only begin with a letter or an underscore only.
- C++ has reserved keywords that cannot be used as identifiers since they have predefined meanings in the language. For example, int cannot be used as an identifier as it has already some predefined meaning in C++. Attempting to use these as identifiers will result in a compilation error.
- Identifier must be unique in its namespace.
- The language imposes no limit on name length.
Example
Invalid Identifier | Bad Identifier | Good Identifier |
---|---|---|
Cash prize | C_prize | cashprize |
catch | catch_1 | catch1 |
1list | list_1 | list1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; // here Car_24 identifier is used to refer the below class class Car_24 { string Brand; string model; int year; }; // calculateSum identifier is used to call the below // function void calculateSum(int a, int b) { int _sum = a + b; cout << "The sum is: " << _sum << endl; } int main() { // identifiers used as variable names int studentAge = 20; double accountBalance = 1000.50; string student_Name = "Karan"; calculateSum(2, 10); return 0; } |
Output:
1 | The sum is: 12 |