In a program, C++ identifiers are used to refer to the names of variables, functions, arrays, or other user-defined data types created by the programmer. They are a basic requirement of any language. Each language has its own rules for naming identifiers.
In short, we can say that C++ identifiers represent the basic elements in a program, as follows:
-
Constants
-
Variables
-
Functions
-
Labels
-
User-defined data types
Some naming rules are common in both C and C++. They are as follows:
-
Only letters, digits, and underscores are allowed.
-
The name of the identifier cannot start with a digit; the first character should be a letter. After the first letter, we can use letters, digits, or underscores.
-
In C++, uppercase and lowercase letters are different. Therefore, we can say that C++ identifiers are case-sensitive.
-
Declared keywords cannot be used as variable names.
For example, suppose we have two identifiers named ‘FirstName’ and ‘Firstname’. The two identifiers will be different because, in the first case, the letter ‘N’ is uppercase, while in the second case it is lowercase. Thus, this proves that identifiers are case-sensitive.
Valid Identifiers
Here are examples of valid identifiers:
ResultTest2_sumpower
👇Click to receive👇
👉C Language Knowledge Resource Collection
Invalid Identifiers
Here are examples of invalid identifiers:
Sum-1 // contains special character "-".
2data // first character is a digit.
break // uses a keyword.
Note: Identifiers cannot be used as keywords. They may not conflict with keywords, but it is strongly advised not to use keywords as identifier names. You should always name identifiers consistently to make your code more readable and maintainable.
The main difference between C and C++ is the length limit for variable names. ANSI C only considers the first 32 characters of the name, while ANSI C++ has no limit on name length.
A constant refers to an identifier that represents a fixed value that does not change during program execution. Both C and C++ support various types of literal constants, and they do not have any memory location. For example, 123, 12.34, 037, 0X2, etc. are all literal constants.
Let’s look at a simple example to understand the concept of identifiers.
#include <iostream>
using namespace std;
int main(){
int a;
int A;
cout<<"Enter the values of 'a' and 'A'";
cin>>a;
cin>>A;
cout<<"\nThe values that you have entered are : "<<a<<" , "<<A;
return 0;
}
In the above code, we declare two variables ‘a’ and ‘A’. The two letters are the same, but they will operate as different identifiers. Because we know that identifiers are case-sensitive, the two identifiers will have different memory locations.
Output:
What is a Keyword?
Keywords are reserved words that have special meanings. They are specifically used for special purposes and cannot be used as identifiers. For example, ‘for’, ‘break’, ‘while’, ‘if’, ‘else’, etc. are predefined words, where predefined words are words known to the compiler. Identifiers are names defined by programmers for program elements (such as variables, functions, arrays, objects, classes).
The difference between identifiers and keywords
Here is a list of differences between identifiers and keywords:
Identifier | Keyword |
---|---|
Identifiers are names of basic elements in a program, defined by the programmer. | Keywords are reserved words known to the compiler. They are used to identify variable names. |
It is used to specify the type of an entity. | It can contain letters, digits, and underscores. |
It only contains letters. | It can use lowercase and uppercase letters. |
It only uses lowercase letters. Special characters cannot be used except for underscores. | It cannot contain any special characters. |
The starting letter of an identifier can be a lowercase letter, uppercase letter, or underscore. | It can only start with a lowercase letter. |
It can be classified into internal identifiers and external identifiers. | It cannot be further classified. |
Examples are test, result, sum, power, etc. | Examples are ‘for’, ‘if’, ‘else’, ‘break’, etc. |
Hot Recommendations
-
C Language Tutorial – Detailed Explanation of Prime Number Program in C Language
-
C Language Algorithm – “Combination Sum II” Algorithm Problem
-
C++ Tutorial – First C++ Program