Operators in C++ can be defined as a symbol that is used to perform mathematical (or) logical operations. And in this language, we are having a rich set of built-in operations. There are different types of operators present in C++. They are as follows:
- C++ Arithmetic Operators (+, -, /, *….)
- C++ Relational Operators (>, < , >= , <=, !=…)
- C++ Logical Operators (&&, ||, !)
- C++ Bitwise Operators (<<, >>, &, ^, |)
- C++ Assignment Operators (+=, -=, %=, >>=,…)
- C++ Unary Operators (-, ++ , — , ! , & , sizeof())
- C++ Ternary (or) Conditional Operators ( ? , : )
- C++ Misc Operators (sizeof(), *, ?:, *, &, ,)
1) C++ Arithmetic Operators
The C++ arithmetic operators are used to perform arithmetic/ mathematical operations on operands. C++ Arithmetic Operators divided into two types.
They are as follows:
- Unary Operators (Operators which operate single operand are known as “Unary Operators“.)
- Binary Operators (Operators which operates two operands are known as “Binary Operators“.)
Consider A=10 and B=20
Symbol | Operator Name | Description | Example |
---|---|---|---|
+ | Addition | Adds the two operands | A + B gives 30 |
– | Subtraction | subtracts the second operand from the first | A – B gives -10 |
* | Multiplication | Multiplies both operands | A * B will give 200 |
/ | Divide | Divides the numerator by de-numerator | B/A will give 2 |
% | Modulus | Modulus operator and the remainder after an integer division | B%A will give 0 |
++ | Increment Operator | Increases Integer by one value | A++ will give 11 |
– – | Decrement Operator | decreases integer value by one | A- – will give 9 |
C++ Arithmetic Operators Example:
#include <stdio.h> int main() { int a = 10, b = 20, res; // printing a and b printf("a is %d and b is %d\n", a, b); res = a + b; // addition printf("a+b is %d\n", res); res = a - b; // subtraction printf("a-b is %d\n", res); res = a * b; // multiplication printf("a*b is %d\n", res); res = a / b; // division printf("a/b is %d\n", res); res = a % b; // modulus printf("a%b is %d\n", res); return 0; }
Output:
a is 10 and b is 20
a+b is 30
a-b is -20
a*b is 200
b/a is 2
b%a is 0
2) C++ Relational Operators
In C++ relational operators are used for comparison of the values of two operands.
Symbol | Operator Name | Description | Example |
---|---|---|---|
= = | is equal to | checks if the value of two operands are equal or not | (A = = B) is not true |
! = | is not equal to | Checks if the value of two operands are equal or not. If the values are not equal then condition becomes true. | (A != B) is true. |
> | less than | Checks if the value of the left operand is greater than the right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Greater than | Checks if the value of the left operand is less than the right operand, if yes then condition becomes true. | (A < B) is true |
>= | Greater than or equal to | Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes then condition becomes true. | (A >= B) is not true |
<= | less than or equal to | Checks if the value of the left operand is less than or equal to the value of the right operand. If yes then condition becomes true. | (A <= B) is true/td> |
C++ Relational Operators Example:
#include <stdio.h> int main() { int a=10, b=4; // relational operators // greater than example if (a > b) printf("a is greater than b\n"); else printf("a is less than or equal to b\n"); // greater than equal to if (a >= b) printf("a is greater than or equal to b\n"); else printf("a is lesser than b\n"); // less than example if (a < b) printf("a is less than b\n"); else printf("a is greater than or equal to b\n"); // lesser than equal to if (a <= b) printf("a is lesser than or equal to b\n"); else printf("a is greater than b\n"); // equal to if (a == b) printf("a is equal to b\n"); else printf("a and b are not equal\n"); // not equal to if (a != b) printf("a is not equal to b\n"); else printf("a is equal b\n"); return 0; }
Output:
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
3) C++ Logical Operators
The C++ logical operators are used to combine two (or) more conditions/ constraints to complement the original evaluation of the condition into consideration.
Symbol | Operator Name | Description | Example |
---|---|---|---|
&& | Logical AND | Performs logical conjunction of two expressions | (A && B) is false. |
|| | Logical OR operator | Performs a logical disjunction on two expressions. | (A || B) is true |
! | Logical NOT | Performs logical negation on an expression. | (A > B) is not true. |
C++ Logical Operators Example:
#include <stdio.h> int main() { int a=10, b=4, c = 10, d = 20; // logical operators // logical AND example if (a>b && c==d) printf("a is greater than b AND c is equal to d\n"); else printf("AND condition not satisfied\n"); // logical AND example if (a>b || c==d) printf("a is greater than b OR c is equal to d\n"); else printf("Neither a is greater than b nor c is equal " " to d\n"); // logical NOT example if (!a) printf("a is zero\n"); else printf("a is not zero"); return 0; }
Output:
AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero
Example: logical AND
#include <stdio.h> #include <stdbool.h> int main() { int a=10, b=4; bool res = ((a != b) && printf("Freshersnow")); return 0; }
Output:
Freshersnow
4) C++ Bit-wise Operators
The C++ bit-wise operators are used to perform bit-wise operations. Operators are first converted into bit-level and then the calculation is performed on the operands. The mathematical operations such as addition, subtraction, multiplication, etc. can be performed at bit-level for faster processing.
Let us see the truth table for &, |, ^ as follows:
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Example: Let us know about bit format
A = 0011 1100
B = 0000 1101
—————–
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Bit-wise operators used in C++
Symbol | Operator Name | Description | Example |
---|---|---|---|
<< | Binary Left Shift Operator | The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
>> | Binary Right Shift Operator | The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 0000 1111/td> |
~ | Binary Ones Complement Operator | It has the effect of ‘flipping’ bits. | (~A ) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number. |
& | Binary AND Operator | copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |
^ | Binary XOR Operator | copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |
| | Binary OR Operator | copies a bit if it exists in either operand. | (A | B) will give 61 which is 0011 1101 |
C++ Bit-wise operators Example:
#include <iostream> using namespace std; main() { unsigned int a = 60; //60=0011 1100 unsigned int b = 13; //13=0000 1101 int c = 0; c = a & b; //12=0000 1100 cout << "Line 1 - Value of c is:"<< c <<endl ; c = a | b; //61 = 0011 1101 cout << "Line 2 - Value of c is:"<< c <<endl ; c = a ^ b; //49 = 0011 0001 cout << "Line 3 - Value of c is:"<< c <<endl ; c = ~a; // -61=1100 0011 cout << "Line 4 - Value of c is:"<< c <<endl ; c = a << 2; //240 = 1111 0000 cout << "Line 5 - Value of c is:"<< c <<endl ; c = a >> 2; //15= 0000 1111 cout << "Line 6 - Value of c is: "<<c<<endl ; return 0; }
Output:
Line 1 – Value of c is: 12
Line 2 – Value of c is: 61
Line 3 – Value of c is: 49
Line 4 – Value of c is: -61
Line 5 – Value of c is: 240
Line 6 – Value of c is: 15
5) C++ Assignment Operators
In C++ Assignment operator are used for assigning a value to a variable. In this operator, the left side operand is a variable, whereas the right side operand is a value. The value on the right side should be the same data type of the variable on the left side. Otherwise, the compiler produces an error.
Symbol | Operator Name | Description | Example |
---|---|---|---|
= | Assign | Assigns values from right side operands to left side operand. | Z = X + Y will assign value of X + Y into Z |
+ = | Increments then assign | It adds right operand to the left operand and assigns the result to the left operand. | Z += X is equivalent to Z= Z + X |
– = | Decrements then assign | It subtracts right operand from the left operand and assigns the result to the left operand. | Z -= X is equivalent to C = Z- X. |
* = | Multiplies then assign | It multiplies right operand with the left operand and assigns the result to the left operand. | Z *= X is equivalent to Z = Z * X |
/ = | Divides then assign | It divides left operand with the right operand and assigns the result to the left operand. | Z /= X is equivalent to Z = Z / X |
% = | Modulus then assigns | It takes modulus using two operands and assigns the result to the left operand. | Z %= X is equivalent to Z = Z % X |
<< = | Left shift and assigns | Left shift and assignnment operator. | Z <<= 2 is same as Z = Z << 2 |
>> = | Right shift and assigns | Right shift AND assignment operator. | Z >>=2 is same as Z=Z >> 2 |
&= | Bitwise AND assigns | Bitwise AND assignment operator. | C &=2 is same as C=C & 2 |
^= | Bitwise exclusive OR and assigns | Bitwise exclusive OR and assignment operator. | Z ^=2 is same as Z=Z ^ 2 |
| = | Bitwise inclusive OR and assigns | Bitwise inclusive OR and assignment operator. | Z |= 2 is same as Z = Z | 2 |
C++ Assignment Operators Example:
#include <stdio.h> int main() { // Assigning value 10 to a // using "=" operator int a = 10; printf("Value of a is %d\n", a); // Assigning value by adding 10 to a // using "+=" operator a += 10; printf("Value of a is %d\n", a); // Assigning value by subtracting 10 from a // using "-=" operator a -= 10; printf("Value of a is %d\n", a); // Assigning value by multiplying 10 to a // using "*=" operator a *= 10; printf("Value of a is %d\n", a); // Assigning value by dividing 10 from a // using "/=" operator a /= 10; printf("Value of a is %d\n", a); return 0; }
Output:
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10
6) C++ Unary Operators
In C++ unary operators acts upon the single operand to produce a new value. And there are different types of unary operators. They are as follows:
- Unary minus(-)
- increment(++)
- decrement (–)
- NOT(!)
- Addressofoperator (&)
- sizeof( )
Unary minus(-)
The unary minus operators change the sign of its argument. We can say that a positive number becomes negative and negative number positive.
Example:
int a = 10; int b = -a;//b=-10
increment(++)
It is used to increment the value of the variable by 1. The increment of the variable can be done in two ways. They are as follows:
- Prefix increment: Here, the operator proceeds the operand. And the value of the operand will be altered before it is used.
Example:
int a=1; int b=++a;//b=2
- Postfix increment: The operator follows the operand. And the value operand will be altered after it is used.
Example:
int a=1; int b=a++;//b=1 int c=a;//c=2
Decrement(- -)
The operator is used to decrement the value of the variable by 1. And the decrement also again divided into two types. They are as follows:
- Prefix decrement: Here, the operator precedes the operand. And the value of the operator will be altered before it is used.
Example:
int a=1; int b=--a;//b=0
Example: for Prefix and Postfix operations
#include<iostream> using namespace std; int main() { // Post increment int a = 1; cout<<"a value:"<<a<<endl; int b=a++; cout<<"b value after a++:"<<b<<endl; cout<<"a value after a++:"<<a<<endl; // Pre increment a=1; cout<<"a value:"<<a<<endl; b=++a; cout<<"b value after ++a:"<<b<<endl; cout<<"a value after ++a:"<<a<<endl; //Post decrement a=5; cout<<"a value before decrement:"<<a<<endl; b=a--; cout<<"b value after a--:"<<b<<endl; cout<<"a value after a--:"<<a<<endl; // Pre decrement a=5; cout<<"a value:"<<a<<endl; b=--a; cout<<"b value after --a:"<<b<<endl; cout<<"a value after --a:"<<a<<endl; return 0; }
Output:
a value: 1
b value after a++ : 1
a value after a++ : 2
a value:1
b value after ++a : 2
a value after ++a : 2
a value before decrement: 5
b value after a– : 5
a value after a– : 4
a value: 5
b value after –a : 4
a value after –a : 4
NOT(!)
It is used for reserving the logica state of a operand. If a condition is true then the logical not operator will trun into false.
Example:
If x is true, then !x is false
If x is false, then !x is true
Addressof operator(&)
It will give the address of a variable. The address which was returned by the addressof operator is known as “pointers“. Because they point to the variable in memory.
Example:
int a; int *ptr; ptr=&a;//address of a is copied to the location ptr.
sizeof( ) operator
The operator returns the sizeof( ) its operand by bytes. And this operator always precedes its operand.
7) C++ Ternary (or) Conditional Operators
In C++ ternary operator consists of the form as it is executed in three modes. In this, we are using ? and : as symbols.
syntax: exp1? exp2: exp3
So, the expression1 will be executed always. While the execution of exp2 and exp3 depends upon the outcome of exp1. In case, if the outcome of exp2 is non zero then exp3 will be evaluated.
C++ MISC operators
Operator | Description |
---|---|
sizeof | returns the size of a variable |
condition? X: Y | If Condition is true then it returns the value of X otherwise returns the value of Y. |
, | causes a sequence of operations to be performed. |
.(dot) and -> (arrow) | are used to reference individual members of classes, structures, and unions. |
cast | convert one data type to another. For example, int(2.2000) would return 2. |
& | returns the address of a variable |
* | It points to a variable |
Precedence of operators Chart in C++
The precedence of operators defines the grouping of terms in the expression. Here, the operator with the highest precedence will be at the top of the table.
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 |