×

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any language. For naming identifiers, each language has its own set of conventions.

In a nutshell, the C++ identifiers reflect the program's basic parts, which are listed below:

  • Constants
  • Variables
  • Functions
  • Labels
  • Data types that have been defined

In both C and C++, there are several naming conventions that are universal. The following are the details:

  • Only letters from the alphabet, numbers, and underscores are permitted.
  • The initial letter of the identification name must be alphabetical and cannot begin with a number. We can use letters, numerals, or underscores after the initial character.
  • Uppercase and lowercase letters are distinguished in C++. As a result, we may claim that identifiers in C++ are case sensitive.
  • A defined keyword can't be used as the name of a variable.

Let see an example: Let's say we have two IDs, 'FirstName' and 'Firstname'. Both IDs will be distinct, with the letter 'N' in uppercase in the first case and lowercase in the second. As a result, it is demonstrated that identifiers are case-sensitive.

Valid Identifiers

The following are some valid identifier:

  1. Conclusion 
  2. Valid2 
  3. _add 
  4. power

Invalid Identifiers

Invalid IDs can be found in the following examples:

Example:

add-1   // containing special character '-'.  
2next    // the first letter is a digit.   
break    // use of a keyword.    

NOTE: Identifiers aren't allowed to be used as keywords. Although it is possible that it will not clash with the keywords, it is strongly advised that the keywords not be used as the identification name. To make your code more understandable and manageable, you should always name the identifiers in the same way.

The main distinction between C and C++ is the length restriction on variable names. In ANSI C, only the first 32 characters of a name are considered, however in ANSI C++, there is no restriction on the length of a name. Constants are identifiers that refer to a constant value that does not change during a program's execution. Literal constants are supported in both C and C++, and they can be stored in any memory region. The literal constants are, for example, 324, 11.13, 007, 1X0, and so on.

To grasp the notion of identifiers, consider the following example:

Example:

#include <iostream>  
#include<bits/sdtc++.h>
using namespace std;  
int main()  
{  
    int x;  
    int Y;  
    cout<<"enter the values of 'x' and 'Y'";  
    cin>>x;  
    cin>>Y;  
    cout<<"\nThe values that you have entered are : "<<x<<" , "<<Y;  
    return 0;  
} 

OUTPUT:

The values that you have entered are:  x, Y

Explanation:

The variables 'x' and 'Y' are declared in the above code. Both letters are identical, however they will function as distinct identities. Because identifiers are case-sensitive, both IDs will be stored in distinct memory locations.

Keywords

The reserved words that have a specific meaning for the compiler are known as keywords. They are set aside for a specific purpose and are not to be used as identifiers. For example, predefined terms are words whose meaning is already known by the compiler, such as 'for', 'break', 'while', 'if', 'otherwise', and so on. Identifiers, on the other hand, are the names given by the programmer to programme components like variables, functions, arrays, objects, and classes. The entire list of C++ keywords may be found in the table below:

autoelseoperatorTemplate
breakenumprivatethis
caseexternprotectedthrow
catchfloatpublictry
charforregistertypedef
classfriendreturnunion
constgotoshortunsigned
continueifsignedvirtual
defaultinlinesizeofVoid
deleteIntstaticvolatile
dolongstructwhile
  • Declares that a section of code should be given to the assembler.
  • auto is a storage class specifier for defining objects in a block.
  • A break statement or a loop is used to end a switch statement or a loop.
  • case: In a switch statement, case is used to define a match for the statement's expression.
  • catch: Defines what happens if an exception occurs.
  • Character objects are defined by the data type char.
  • Declare a user-defined type that encompasses data members, actions, and member functions as a class.
  • Const: To declare objects whose value will not change throughout the execution of the programme.
  • continue:- Returns control to the beginning of the loop.
  • default:- Handles expression values that aren't handled by case in a switch statement.
  • Memory deallocation operator delete: Memory deallocation operator.
  • do: denotes the commencement of a do-while statement, in which the sub-statement is performed until the expression's value is logical-false.
  • double: The most basic data type for defining a floating-point value.
  • else: A keyword that is only used in if-else statements.
  • To specify a user-defined enumeration data type, use the enum keyword.
  • extern: An identifier marked as extern is linked to the block from the outside.
  • float: The most basic data type for defining a floating-point value.

Related Topics

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.