In C++ programming language, identifiers are the unique names assigned to variables, functions, classes, structs, or other entities within the program. Let's take a look at an example:
C++
// Creating a variable
int val = 10;
// Creating a function
void func() {}
In the above code, the words val and func are identifiers. Basically, everything named by a programmer is an identifier and is used to refer to the entity later in the program.
Rules to Name of an Identifier
We can use any word as an identifier as long as it follows the following rules:
- 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 already has some predefined meaning in C++. Attempting to use these as identifiers will result in a compilation error.
- Identifier must be unique in its namespace.
Additionally, C++ is a case-sensitive language so the identifier such as Num and num are treated as different. The below images show some valid and invalid C++ identifiers.
To know more about identifiers naming rules, refer to this article – Naming Convention in C++
Example of Valid/Invalid IdentifiersExample
In this example, we have used the identifiers by following the guidelines and we use identifiers to name a class, function, integer data type, etc. If you are not aware of functions and classes in C++ then don’t worry, you will learn them soon. The below code is run successfully which means we named them correctly.
C++
#include <iostream>
using namespace std;
// Here Car identifier is used to refer to below class
class Car {
string Brand;
string model;
int year;
};
// getSum identifier is used to call the below
// function
void getSum(int a, int b) {
int _sum = a + b;
cout << "The sum is: " << _sum;
}
int main() {
// Identifiers used as variable names
int studentAge = 20;
double accountBalance = 1000.50;
string student_Name = "Karan";
getSum(2, 10);
return 0;
}
Identifier Naming Conventions
Naming conventions are not the rules enforced by C++ language but are suggestions for naming variables by the programming community for easier understanding. Some of the naming conventions are as follows:
For Variables:
- Use camelCase (for constants, you can use UPPER_SNAKE_CASE)
- Start with lowercase alphabet.
- Use descriptive, meaningful names.
- For example, frequencyCount, personName
For Functions:
- Use camelCase.
- Use Verb or verb phrases for naming.
- For example, getName(), countFrequency(), etc.
For Classes:
- Use PascalCase
- Use Nouns or noun phrases for naming.
- For example, Car, Person, etc
Once again, above are some suggestions for naming identifiers do not rule. They also depend on the project guidelines you are working on and your preferences.
Similar Reads
C Identifiers In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any other user-defined items. It is a name that uniquely identifies a program element and can be used to refer to it later in the program.Example:C// Creating a variable int val = 10; // Creatin
3 min read
C++ Pointers A pointer is a variable that stores the address of another variable. Pointers can be used with any data type, including basic types (e.g., int, char), arrays, and even user-defined types like classes and structures.Create PointerA pointer can be declared in the same way as any other variable but wit
8 min read
Literals in C++ In C++ programming language, literals are fundamental elements used to represent fixed values. These values can include numbers, characters, strings, and more. They are generally present as the right operand in the assignment operation.Let's take a look at an example:C++#include <iostream> usi
6 min read
Literals in C In C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably. For example, âconst int =
4 min read
C++ Type Modifiers In C++, type modifiers are the keywords used to change or give extra meaning to already existing data types. It is added to primitive data types as a prefix to modify their size or range of data they can store.C++ have 4 type modifiers which are as follows:Table of Contentsigned Modifierunsigned Mod
4 min read
What are Identifiers in Programming? Identifiers are names given to various programming elements, such as variables, functions, classes, constants, and labels. They serve as labels or handles that programmers assign to program elements, enabling them to refer to these elements and manipulate them within the code. In this article, we wi
3 min read
Variables in Objective-C A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
4 min read
C++ Tokens In C++, tokens can be defined as the smallest building block of C++ programs that the compiler understands. It is the smallest unit of a program into which the compiler divides the code for further processing. In this article, we will learn about different types of tokens in C++. Types of Tokens in
5 min read
C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
C Preprocessor Directives In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll
6 min read