C++ Identifiers The Complete Guide for Beginners 2022

C++ Identifiers

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 represent the program’s basic parts, which are listed below:

  • Constants
  • Variables
  • Functions
  • Labels
  • Defined data types

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

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

Consider the following scenario: we have two identifiers named ‘FirstName’ and ‘Firstname’. Both identifiers 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.

Identifiers that are valid in C++
The following are some valid identifier examples:

  1. Result  
  2. Test2  
  3. _sum  
  4. power  

Identifiers that aren’t valid
Invalid IDs can be found in the following examples:

  1. Sum-1   // containing special character ‘-‘.  
  2. 2 data    // the first letter is a digit.   
  3. break    // use of a keyword.    

It’s worth noting that identifiers can’t 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 maintainable, you should always name the identifiers in the same way.

It’s important to note that identifiers aren’t allowed to be used as keywords. It is strongly recommended that the keywords not be used as the identification name, even if it is feasible that it will not clash with the keywords. You should always name your identifiers the same manner to make your code more comprehensible and maintainable.

To grasp the concept of identifiers, consider the following example.

The variables ‘a’ and ‘A’ are declared in the above code. Both letters are identical, however they will function as distinct identities. Because identifiers are case-sensitive, both identifiers will be stored in distinct memory locations.

Output:

What are the Search Keywords?
The reserved words that have a special 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 program elements like variables, functions, arrays, objects, and classes.

Identifiers and Keywords: What’s the Difference?

Here is a list of distinctions between identifiers and keywords:

IdentifiersKeywords
Identifiers are the names defined by the programmer to the basic elements of a program.Keywords are the reserved words whose meaning is known by the compiler.
It is used to identify the name of the variable.It is used to specify the type of entity.
It can consist of letters, digits, and underscore.It contains only letters.
It can use both lowercase and uppercase letters.It uses only lowercase letters.
No special character can be used except the underscore.It cannot contain any special character.
The starting letter of identifiers can be lowercase, uppercase or underscore.It can be started only with the lowercase letter.
It can be classified as internal and external identifiers.It cannot be further classified.
Examples are test, result, sum, power, etc.Examples are ‘for’, ‘if’, ‘else’, ‘break’, etc.

C++ Expression

C++ expressions are made up of operators, constants, and variables that are organized according to the language’s rules. It may also include function calls that return results. To compute a value, an expression can include one or more operands and zero or more operators. Every expression generates a value, which is then assigned to a variable using the assignment operator.

C++ expression examples:

There are several types of expressions:

  • Constant expressions
  • Integral expressions
  • Float expressions
  • Pointer expressions
  • Relational expressions
  • Logical expressions
  • Bitwise expressions
  • Special assignment expressions

Compound expressions are expressions that are made up of a combination of the above expressions.

Expressions that never change
A constant expression is one in which all of the values are constant. It’s a type of expression that has its value determined at compile time but is evaluated at run time. Integer, character, floating-point, and enumeration constants can all be used.

In the following situations, constants are used:

  • It is used to describe the array bound in the subscript declarator.
  • In the switch statement, it comes after the case keyword.
  • It’s a number value in an enumeration.
  • It specifies the width of a bit-field.
  • It’s used in the #if preprocessor.
  • Constant expressions with integer, character, and enumeration constants can be used in the cases above. To define the function-scope, we can utilize the static and extern keywords with constants.

The expression with a constant value is shown in the table below:

Expression containing constantConstant value
x = (2/3) * 4(2/3) * 4
extern int y = 6767
int z = 4343
static int a = 5656

Let’s have a look at a basic program with constant expressions:

First, we declared the integer-type variable ‘x’ in the code above. We assign the basic constant expression to the ‘x’ variable after declaring it.

Output

Integral Expressions

After executing all of the explicit and implicit conversions, an integer expression delivers the integer value as output.

The following are some integral expression examples:

Let’s look at a basic integral expression:

We’ve declared three variables in the code above: x, y, and z. We take user input for the values of ‘x’ and ‘y’ after declaring them. Then we sum the ‘x’ and ‘y’ values and save the result in the ‘z’ variable.

Output:

Float Expressions

A float expression is an expression that, after all explicit and implicit conversions, returns a floating-point value as an output.

The following are some float expression examples:

Let’s have a look at an example.

#include
using namespace std;
int main()
{

float x=8.9; // variable initialization
float y=5.6; // variable initialization
float z; // variable declaration
z=x+y;
std::cout <<"value of z is :" << z<

Output

Pointer Expressions

A pointer expression is a type of expression that outputs an address value.

Let’s look at an example to help you understand.

&x
ptr
ptr++
ptr-

Let’s have a look at an example.

#include
using namespace std;
int main()
{

int a[]={1,2,3,4,5}; // array initialization
int *ptr; // pointer declaration
ptr=a; // assigning base address of array to the pointer ptr
ptr=ptr+1; // incrementing the value of pointer
std::cout <<"value of second element of an array : " << *ptr<

The array and a pointer ptr are declared in the above code. The variable ‘ptr’ is given the base address. We increment the value of pointer ‘ptr’ after assigning the address. When the pointer is incremented, ‘ptr’ will point to the array’s second element.

Output

Relational Expressions

A relational expression is an expression that returns a bool value, which can be true or false. A boolean expression is another name for it. When arithmetic expressions are employed on both sides of the relational operator, the outcomes of the arithmetic expressions are compared first.

The following are some relational expression examples:

Let’s look at an example to help you understand.

#include
using namespace std;
int main()
{
int a=45; // variable declaration
int b=78; // variable declaration
bool y= a>b; // relational expression
cout<<"Value of y is :"<

Two variables, ‘a’ and ‘b,’ have been declared in the above code. We used the relational operator between the variables after declaring them to see if ‘a’ is greater than ‘b’ or not.

Output

Let’s see another example.

#include
using namespace std;
int main()
{
int a=4; // variable declaration
int b=5; // variable declaration
int x=3; // variable declaration
int y=6; // variable declaration
cout<<((a+b)>=(x+y)); // relational expression
return 0;
}

We’ve declared four variables in the above code: ‘a’, ‘b’, ‘x’, and ‘y’. The relational operator (>=) is then used between these variables.

Output

Logical Expressions

A logical expression is an expression that produces a bool type value by combining two or more relational expressions. ‘&&’ and ‘||’ are logical operators that join two or more relational expressions.(C++ Identifiers The Complete Guide for Beginners 2022)

Some instances of logical expressions are as follows:

Let’s look at an example to help you understand.

#include
using namespace std;
int main()
{
int a=2;
int b=7;
int c=4;
cout<<((a>b)||(a>c));
return 0;
}

Output

Bitwise Expressions

A bitwise expression is a type of expression that manipulates data at the bit level. They’re primarily employed to shuffle bits around.

The value of ‘x’ in the previous example is 3 and its binary value is 0011. The value of ‘x’ is being shifted three bits to the right. Let’s look at it from a diagrammatic perspective.

Let’s look at an example to help you understand.

#include
using namespace std;
int main()
{
int x=5; // variable declaration
std::cout << (x>>1) << std::endl; return 0; }

A variable named ‘x’ has been declared in the code above. We used the bitwise operator, i.e., right shift operator, to move one-bit position to the right after declaring.(C++ Identifiers The Complete Guide for Beginners 2022)

Output

Special Assignment Expressions

Special assignment expressions are expressions that can be further categorised based on the variable value assigned to it.

Assignment in a Chain
A chained assignment expression is one in which a single statement is used to assign the same value to many variables.

For example:

  1. a=b=20   
  2.  or   
  3. (a=b) = 20  

Let’s look at an example to help you understand.

#include
using namespace std;
int main()

int a; // variable declaration
int b; // variable declaration
a=b=80; // chained assignment
std::cout <<"Values of 'a' and 'b' are : " <

Two variables, ‘a’ and ‘b,’ have been declared in the above code. Then, using a chained assignment expression, we assigned the identical value to both variables.(C++ Identifiers The Complete Guide for Beginners 2022)

Output

The value cannot be assigned to the variable at the moment of definition when using a chained assignment expression. Int a=b=c=90, for example, is an incorrect statement.
Assignment Expression Embedded
The term “embedded assignment expression” refers to an assignment expression that is enclosed within another assignment expression.

Let’s look at an example to help you understand.

#include
using namespace std;
int main()
{
int a; // variable declaration
int b; // variable declaration
a=10+(b=90); // embedded assignment expression
std::cout <<"Values of 'a' is " <

Two variables, ‘a’ and ‘b,’ have been declared in the above code. The embedded assignment statement (a=10+(b=90) was then used.(C++ Identifiers The Complete Guide for Beginners 2022)

Output

Assignment (Compound)
An expression that combines an assignment operator and a binary operator is known as a compound assignment expression.

For example,

  1. a+=10;   

‘a’ is a variable in the above expression, and ‘+=’ is a compound statement.

Let’s look at an example to help you understand.

#include
using namespace std;
int main()
{
int a=10; // variable declaration
a+=10; // compound assignment
std::cout << "Value of a is :" <

We have declared a variable ‘a’ and assigned a value of 10 to it in the given code. After that, we used the compound assignment operator (+=) on the ‘a’ variable, resulting in a+=10, which equals (a=a+10). The value of ‘a’ is increased by ten in this expression.

Output

Summary

In a program, C++ Identifiers The Complete Guide for Beginners 2022 relate to the names of variables, functions, arrays, and other user-defined data types. For naming identifiers, each language has its own set of conventions. Only characters from the alphabet, numbers, and underscores are permitted.

Related Articles

Inquiries

If you have any questions or suggestions about C++ Identifiers The Complete Guide for Beginners 2022, please feel free to leave a comment below.

Leave a Comment