×

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 various different types of operators. Operators are C++ symbols that instruct the compiler to carry out particular relational, logical, or mathematical operations. Operands are values or variables that undergo an operation.

Example Syntax:

int sum = 10 + 5;

Here, + is the operator and 5 and 10 are the operands.

In C++, various operators are available and are classified based on their functionality.

Types of C++ Operators:

  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Assignment Operators
  • Bitwise Operators
  • Misc. Operators

1. Arithmetic Operators:

Arithmetic operators are used to perform basic mathematical operation such as addition (+), subtraction (-), multiplication (*), division (/) etc.

OperatorDescriptionExample(x=5,y=5)Result
+Addition operatorx+y10
-Subtraction operatorx-y0
*Multiplication operatorx*y25
/Division operatorx/y1
%Modulus (Remainder) operatorx%y0

Example:

Let us take an example to illustrate the arithmetic operators in C++.

#include <iostream>

using namespace std;

int main() {

    int value1 = 5, value2 = 5;

    cout << "Addition: " << (value1 + value2) << endl;

    cout << "Subtraction: " << (value1 – value2) << endl;

    cout << "Multiplication: " << (value1 * value2) << endl;

    cout << "Division: " << (value1 / value2) << endl;

    cout << "Modulus: " << (value1 % value2) << endl;

    return 0;

}

Output:

Addition: 10

Subtraction: 0

Multiplication: 25

Division: 1

Modulus: 0

2. Logical operators:

Logical operators can be used to combine numerous variables and generate a Boolean (true or false) result.

OperatorDescriptionExample (x=true, y=false)Result
&&Logical ANDx&&yFalse
||Logical ORx||yLogical OR
!Logical NOTx!yFalse

Example:

Let us take an example to illustrate the logical operators in C++.

#include <iostream>

using namespace std;

int main() {

    bool x = true, y = false;

    cout << "x && y: " << (x && y) << endl;

    cout << "x || y: " << (x || y) << endl;

    cout << "!x: " << (!x) << endl;

    return 0;

}

Output:

x && y: 0

x || y: 1

!x: 0

3. Relational Operators:

Relational operations compare two values and return true or false. Relational operator shows the relation and compare between operands. Following are the different relational operations.

OperatorDescriptionExample (x=10, y= 20)Result
==Equal tox==yFalse
!=Not Equal tox!=yTrue
Greater thanx>yTrue
Less thanx<yFalse
>=Greater than equal tox>=yTrue
<=Less than equal tox<=yFalse

Example:

Let us take an example to illustrate the Relational operators in C++.

#include <iostream>

using namespace std;

int main() {

    int x = 10, y = 20 ;

    cout << "x == y: " << (x == y) << endl;

    cout << "x != y: " << (x != y) << endl;

    cout << "x > y: " << (x > y) << endl;

    cout << "x < y: " << (x < y) << endl;

    cout << "x >= y: " << (x >= y) << endl;

    cout << "x <= y: " << (x <= y) << endl;

    return 0;

}

Output:

x == y: 0

x != y: 1

x > y: 0

x < y: 1

x >= y: 0

x <= y: 1

4. Assignment Operators:

Assignment operators “=” assign values and modify variables using arithmetic operations. The assignment of value takes from right to left.

OperatorDescriptionExample (i=5)Equivalance
=Assigni=5i=5
+=Add and assigni+=5i=i+5
-=Subtract and assigni-=5i=i-5
*=Multiply and assigni*=5i=i*5
/=Divide and assigni/=5i=i/5
%=Modulus and assigni%=5i=i%5

Example:

Let us take an example to illustrate the Assignment operators in C++.

#include <iostream>

using namespace std;

int main() {

    int i = 10;

    i += 5;

    cout << "After i += 5: " << i << endl;

    i *= 2;

    cout << "After i *= 2: " << i << endl;

    return 0;

}

Output:

After i += 5: 15

After i *= 2: 30

5. Bitwise operator:

These Bitwise operators operate at the bit level. It is used to change individual bits into a number. Bitwise operators modify variable according to bit patterns. Following are the different bitwise operators:

OperatorDescriptionExample (i= 10 (1010), j=12(1100))
&Bitwise ANDi&j
||Bitwise OR
^Bitwise XORi^j
~Bitwise NOTi~j
<< Left Shifti<<j
>> Right Shifti>>j

Example:

Let us take an example to illustrate the Bitwise operators in C++.

#include <iostream>

using namespace std;

int main() {

    int i = 5, j = 3;

    cout << "i & j: " << (i & j) << endl;

    cout << "i | j: " << (i | j) << endl;

    cout << "i ^ j: " << (i ^ j) << endl;

    cout << "Left shift i << 1: " << (i << 1) << endl;

    cout << "Right shift j >> 1: " << (j >> 1) << endl;

    return 0;

}

Output:

i & j: 1

i | j: 7

i ^ j: 6

Left shift i << 1: 10

Right shift i >> 1: 2

6. Misc Operators:

Sizeof (sizeof): It returns the size (in bytes) of a variable or data type.

cout << sizeof(int); // Output: 4 (depends on system)

Pointer Operators (*, &): It is used for pointers and memory addresses.

int a = 10, *p = &a; 

cout << *p;  // Output: 10 

Comma Operator (,): It evaluates multiple expressions from left to right.

int x = (5, 10); // x = 10 

Cast Operators ((type), static_cast<>): It convert data types explicitly.

double d = 5.5; 

int x = (int)d; 

7. Precedence Operator:

The order in which an expression’s operators are evaluated depends on operator precedence. Higher priority operators are carried out before lower priority ones.

Example:

int result = 10 + 5 * 2;

The following table shows C++ operators in decreasing order of precedence:

PrecedenceOperatorDescriptionAssociativity
1 (Highest)      ::Scope resolutionLeft to Right
2     ++ --Post-increment, Post-decrementLeft to Right
3++ -- + - ! ~ * & sizeof type()Pre-increment, Pre-decrement, Unary plus/minus, Logical NOT, Bitwise NOT, Dereference, Address-of, Sizeof, TypecastRight to Left
4* / %Multiplication, Division, ModulusLeft to Right
5+ -Addition, SubtractionLeft to Right
6<< >>Bitwise Left and Right shiftLeft to Right
7< <= > >=Relational (less than, greater than, etc.)Left to Right
8== !=Equality and InequalityLeft to Right
9&Bitwise ANDLeft to Right
10^Bitwise XORLeft to Right
11|Bitwise ORLeft to Right
12&&Logical ANDLeft to Right
13||Logical ORLeft to Right
14?:Ternary (Conditional) OperatorRight to Left
15= += -= *= /= %= &= `= ^= <<= >>=`Assignment Operators
16 (Lowest),Comma Operator (Separates expressions)Left to Right

Conclusion:

In conclusion, operators play a very important part of C++ because they help in many computations, logical operations, and value manipulations done in a program. Different types of operators include bitwise, assignment, logical, relational, arithmetic, and many all with some individual significance. It is also important to understand operator precedence and associativity for proper evaluation of expressions. An operator with higher precedence gets evaluated first, while operators with the same precedence apply associativity rules. Precedence can be clear with the use of brackets, which also improves clarity and removes doubt. It helps programmers write meaningful codes in C++ by using the language construct effectively.


Related Topics

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

Factorial of a Number in C++ using while Loop

What is a factorial? The factorial of a number is the product of all the positive numbers less than or equal to n, indicated by n! According to the standard for an...

6 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.