100% found this document useful (1 vote)
7 views6 pages

Operators in C++ - GeeksforGeeks

The document provides an overview of operators in C++, explaining their role in performing mathematical and logical computations. It categorizes operators into six types: Arithmetic, Relational, Logical, Bitwise, Assignment, and Ternary, with examples for each. Additionally, it highlights the differences between unary, binary, and ternary operators, as well as important points regarding their usage.

Uploaded by

Ibrahim Adama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
7 views6 pages

Operators in C++ - GeeksforGeeks

The document provides an overview of operators in C++, explaining their role in performing mathematical and logical computations. It categorizes operators into six types: Arithmetic, Relational, Logical, Bitwise, Assignment, and Ternary, with examples for each. Additionally, it highlights the differences between unary, binary, and ternary operators, as well as important points regarding their usage.

Uploaded by

Ibrahim Adama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks

Search...

Operators in C++
C++ Last
Data Updated
Types C++ Sign In
: 20Input/Output
Jan, 2025 C++ Arrays C++ Pointers C++ OOPs C++ STL C++ In

In C++, an operator is a symbol that operates on a value to perform specific


mathematical or logical computations on given values. They are the foundation
of any programming language.

Example:

#include <iostream>
using namespace std;
int main() {

int a = 10 + 20;

cout << a;
return 0;
}

Output

30

Explanation: Here, ‘+‘ is an addition operator and does the addition of 10 and
20 operands and return value 30 as a result.

In C++, operators are classified into 6 types on the basis of type of operation
they perform:

Table of Content
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Ternary or Conditional Operators

https://www.geeksforgeeks.org/operators-in-cpp/ 1/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks

1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic or mathematical
operations on the operands. For example, ‘+’ is used for addition.

Name Symbol Description

Addition + Adds two operands.

Subtraction – Subtracts second operand from the first.

Multiplication * Multiplies two operands.

Division / Divides first operand by the second operand.

Modulo Operation % Returns the remainder an integer division.

Increment Increase the value of operand by 1.


++

Decrement — Decrease the value of operand by 1.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
int a = 8, b = 3;

// Addition
cout << "a + b = " << (a + b) << endl;

// Subtraction
cout << "a - b = " << (a - b) << endl;

// Multiplication
cout << "a * b = " << (a * b) << endl;

// Division
cout << "a / b = " << (a / b) << endl;

// Modulo
cout << "a % b = " << (a % b) << endl;

https://www.geeksforgeeks.org/operators-in-cpp/ 2/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks
// Increament
cout << "++a = " << ++a << endl;

// Decrement
cout << "--b = " << --b;

return 0;
}

Output

a + b = 11
a - b = 5
a * b = 24
a / b = 2
a % b = 2
++a = 9
--b = 2

Important Points:

The Modulo operator (%) operator should only be used with integers. Other
operators can also be used with floating point values.
++a and a++, both are increment operators, however, both are slightly
different. In ++a, the value of the variable is incremented first and then It is
used in the program. In a++, the value of the variable is assigned first and
then It is incremented. Similarly happens for the decrement operator.

You may have noticed that some operator works on two operands while other
work on one. On the basis of this operators are also classified as:

Unary: Works on single operand.


Binary: Works on two operands.
Ternary: Works on three operands.

2. Relational Operators
Relational operators are used for the comparison of the values of two
operands. For example, ‘>’ check right operand is greater.

https://www.geeksforgeeks.org/operators-in-cpp/ 3/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks

Name Symbol Description

Is Equal To == Checks both operands are equal

Greater Than Checks first operand is greater than the second


>
operand

Greater Than or Checks first operand is greater than equal to


>=
Equal To the second operand

Less Than Checks first operand is lesser than the second


<
operand

Less Than or Equal Checks first operand is lesser than equal to the
<=
To second operand

Not Equal To != Checks both operands are not equal

Example

#include <bits/stdc++.h>
using namespace std;

int main() {
int a = 6, b = 4;

// Equal operator
cout << "a == b is " << (a == b) << endl;

// Greater than operator


cout << "a > b is " << (a > b) << endl;

// Greater than Equal to operator


cout << "a >= b is " << (a >= b) << endl;

// Lesser than operator


cout << "a < b is " << (a < b) << endl;

// Lesser than Equal to operator


cout << "a <= b is " << (a <= b) << endl;

// Not equal to operator


cout << "a != b is " << (a != b);

return 0;
}

https://www.geeksforgeeks.org/operators-in-cpp/ 4/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks

Output

a == b is 0
a > b is 1
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1

Note: 0 denotes false and 1 denotes true.

3. Logical Operators
Logical operators are used to combine two or more conditions or constraints or
to complement the evaluation of the original condition in consideration. The
result returns a Boolean value, i.e., true or false.

Name Symbol Description

Logical Returns true only if all the operands are true or non-
&&
AND zero.

Logical OR Returns true if either of the operands is true or non-


||
zero.

Logical NOT ! Returns true if the operand is false or zero.

Example:

#include <bits/stdc++.h>
using namespace std;

int main() {
int a = 6, b = 4;

// Logical AND operator


cout << "a && b is " << (a && b) << endl;

// Logical OR operator
cout << "a || b is " << (a || b) << endl;
https://www.geeksforgeeks.org/operators-in-cpp/ 5/15
4/24/25, 12:23 PM Operators in C++ | GeeksforGeeks

// Logical NOT operator


cout << "!b is " << (!b);

return 0;
}

Output

a && b is 1
a || b is 1
!b is 0

4. Bitwise Operators
Bitwise operators are works on bit-level. So, compiler first converted to bit-
level and then the calculation is performed on the operands.

Name Symbol Description

Binary AND Copies a bit to the evaluated result if it exists in


&
both operands

Binary OR Copies a bit to the evaluated result if it exists in


|
any of the operand

Binary XOR Copies the bit to the evaluated result if it is present


^
in either of the operands but not both

Left Shift Shifts the value to left by the number of bits


<<
specified by the right operand.

Right Shift Shifts the value to right by the number of bits


>>
specified by the right operand.

One’s
~ Changes binary digits 1 to 0 and 0 to 1
Complement

Note: Only char and int data types can be used with Bitwise Operators.

https://www.geeksforgeeks.org/operators-in-cpp/ 6/15

You might also like