×

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

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 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++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 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.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 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.