C++ Operator: The Complete Guide for Beginners 2022

C++ Operator

With the help of examples, we will learn about the many types of operators in C++ in this tutorial. An operator in programming is a symbol that performs operations on a value or variable.

Symbols that execute operations on variables and values are known as operators. For instance, the addition operator + is used for addition, while the subtraction operator – is used for subtraction.

There are six different types of operators in C++
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

Arithmetic Operators in C++

To perform arithmetic operations on variables and data, arithmetic operators are used. As an example,

The + operator is used to combine two variables, x and y. In C++, there are a variety of different arithmetic operators.

Assume variable A has a value of 12 and variable B has a value of 15.

OperatorMeaning of OperatorDescriptionExample
+addition or unary plusAdds two operands to the equation.X + Y= 27
subtraction or unary minusThe second operand is subtracted from the first.X – Y= -3
*multiplicationBoth operands are multiplied.X * Y= 180
/divisionNumerator is divided by de-numerator.Y / X = 1
%remainder after division (modulo division)After an integer division, the remainder of the modulus operator.Y % X = 3
++Increases Increment The increment operator adds one to the integer value.X++ = 13
Decreases Increment The increment operator reduces the value of an integer by one.Y– = 14

Example of Arithmetic Operators


As we might assume, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.

Increment and Decrement Operators

The increment and decrement operators in C++ are ++ and –, respectively.

++ increases the operand’s value by one.
— reduces it by one

For example,

The code ++num; in this case increases the value of num by one.


We utilized the ++ and — operators as prefixes in the aforementioned program (++x and —y). These operators can also be used as postfixes (x++ and –y).

Assignment Operators in C++

Assignment Operators in C

Assignment operators are used to give a variable a value. The assignment operator’s left side operand is a variable, while the assignment operator’s right side operand is a value. Otherwise, the compiler will throw an error if the value on the right side is not of the same data-type as the variable on the left side.

=This is a straightforward assignment operator. Values from the right side operands are assigned to the left side operand.C = A + B will assign the value of A + B to C
+=The AND assignment operator is used to combine two variables. The right operand is added to the left operand, and the result is assigned to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment is a combination of subtraction and assignment. It takes the right operand and subtracts it from the left operand before assigning the result to the left operand.C -= A is equivalent to C = C – A
*=The AND operator multiplies and assigns. It adds the right and left operands together and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=The AND operator divides and assigns. The left operand is divided by the right operand, and the result is assigned to the left operand.C /= A is equivalent to C = C / A
%=The modulus AND assignment operator is a combination of the modulus and the assignment operator. It uses two operands to compute modulus and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Operator for both the left shift AND the assignment.C <<= 2 is same as C = C << 2
>>=Operator for both the right shift and the assignment.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2

Example of Assignment Operators


Relational Operators in C++

To check the relationship between two operands, a relational operator is utilized. As an example,

The relational operator > is used here. It determines whether x is greater than y.

If the relationship is true, it returns 1, but if it is false, it returns 0.

==Is Equal To5== 9 is false
!=Not Equal To5!= 9 is true
>Greater Than5> 9 is false
<Less Than5 < 9 is true
>=Greater Than or Equal To5 >= 9 is false
<=Less Than or Equal To5 <= 9 is true

Example for Relational Operators


Logical Operators in C++

To determine whether an expression is true or false, logical operators are utilized. If the expression is true, it will return 1, but if it is false, it will return 0.

&&It’s known as the logical AND operator. The condition is true if both operands are non-zero.(A && B) is false.
||The Logical OR Operator is a term used to describe a type of logic. The condition becomes true if any of the two operands is non-zero.(A || B) is true.
It’s known as the Logical NOT Operator. Its operand’s logical state is reversed using it. If a condition is true, the Logical NOT operator will turn it into a false condition.
!(A && B) is true.

In C++, logical operators are frequently used to make decisions. Let’s look at some examples of logical operators to help you grasp them better.

Example of Logical Operators


Because both operands (10!= 21) and (10 < 5) are 1, the expression (3!= 5) && (3 < 5) evaluates to 1. (true).
Because the operand (3 == 5) is 0, the expression (3 == 5) evaluates to 0. (false).
Because both operands (3 == 5) and (3 > 5) are 0, the expression (3 == 5) && (3 > 5) evaluates to 0. (false).
Because both operands (3!= 5) and (3 < 5) are 1, (3!= 5) || (3 5) evaluates to 1. (true).
Because the operand (3!= 5) is 1, the expression (3 > 5) evaluates to 1. (true).
Because both operands (3 == 5) and (3 > 5) are 0, (3 == 5) || (3 > 5) evaluates to 0. (false).
Because the operand (5 == 2) is 0, the result is 1. (false).
Because the operand (5 == 5) is 1, (5 == 5) evaluates to 0. (true).

Bitwise Operators in C++

Bitwise operators are used in C++ to execute operations on single bits. They’re only compatible with char and int data types.

&Binary AND
|Binary OR
^Binary XOR
~Binary One’s Complement
<<Binary Shift Left
>>Binary Shift Right
Because the Arithmetic-Logic Unit (ALU) in the computer’s CPU performs arithmetic operations at the bit level, these operators are required.

Bitwise AND Operator

If and only if both operands are 1, the bitwise AND & operator yields 1. Otherwise, it returns a value of 0.

The bitwise AND operator is demonstrated in the following table. Let a and b be two operands that can only take binary values of 1 and 0, respectively.

xyx & y

Example of Bitwise AND


Other C++ Operators

The following is a list of some more common C++ operators. In coming tutorials, we’ll learn more about them.

sizeofreturns the size of data typesizeof(int); // 4
?:returns value based on the conditionstring result = (5 > 0) ? "even" : "odd"; // "even"
&represents memory address of the operand&num; // address of num
.accesses members of struct variables or class objectss1.marks = 92;
->used with pointers to access the class or struct variablesptr->marks = 92;
<<prints the output valuecout << 5;
>>gets the input valuecin >> num;

Operators Precedence in C++

The order in which terms in an expression are grouped is determined by operator precedence. This has an impact on how an expression is judged. Certain operators take precedence over others; the multiplication operator, for example, takes precedence over the addition operator.

For example, x = 7 + 3 * 2; because operator * has higher precedence than +, x is allocated 13, not 20, and is multiplied by 3*2 before being added to 7.

The highest-priority operators appear at the top of the table, while the lowest-priority operators appear at the bottom. Higher precedence operators will be evaluated first within an expression.

Category Operator Associativity 
Postfix () [] -> . ++ – –  Left to right 
Unary + – ! ~ ++ – – (type)* & sizeof Right to left 
Multiplicative  * / % Left to right 
Additive  + – Left to right 
Shift  << >> Left to right 
Relational  < <= > >= Left to right 
Equality  == != Left to right 
Bitwise AND Left to right 
Bitwise XOR Left to right 
Bitwise OR Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left 
Comma Left to right 


A symbol that operates on a variable or value is known as an operator. Operators include arithmetic, logical, conditional, relational, bitwise, and assignment operators, among others Reference operator, and other specific sorts of operators are also available in C++.

Related Articles


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

Leave a Comment