Operators in C++ | With Examples

With the aid of examples, we will learn about the various operators in C++ in this tutorial. An operator in programming is a symbol that performs an operation on a value or variable.

Operator symbols perform operations on values and variables. For instance, the operators + and – are used for addition and subtraction, respectively.

Six categories can be used to categorize operators in C++:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Other Operators

Without the use of operators, the C programming language cannot function fully.

operators in c++

Arithmetic Operators in C++

Arithmetic operators are used to performing operations on variables and data. For instance,

x + y;

Here, the two variables x and y are added using the + operator. Similar to this, C++ has a number of other Arithmetic operators.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
SubtractionSubtracts one value from anotherx – y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
DecrementDecreases the value of a variable by 1–x

EXAMPLE:

#include <iostream>
using namespace std;
int main() {
    int a, b;
    a = 66;
    b = 44;
	//Program by GeektoCode
    // printing the sum of a and b
    cout << "a + b = " << (a + b) << endl;
    // printing the difference of a and b
    cout << "a - b = " << (a - b) << endl;
    // printing the product of a and b
    cout << "a * b = " << (a * b) << endl;
    // printing the division of a by b
    cout << "a / b = " << (a / b) << endl;
    // printing the modulo of a by b
    cout << "a % b = " << (a % b) << endl;
    return 0;
}
OUTPUT:
a + b = 110
a - b = 22
a * b = 2904
a / b = 1
a % b = 22

Increment and Decrement Operators

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

  • ++ increases the operand’s value by 1;
  • –decreases it by 1.

Assignment Operators in C++

Assignment operators are used in C++ to give variable values. For instance,

// assign 10 to a
a = 10;

The variable a has been given a value of 10 in this instance.

OperatorExampleEquivalent to
=a = b;a = b;
+=a += b;a = a + b;
-=a -= b;a = a – b;
*=a *= b;a = a * b;
/=a /= b;a = a / b;
%=a %= b;a = a % b;

EXAMPLE:

#include <iostream>
using namespace std;
int main() {
    int a, b;
    // 66 is assigned to a
    a = 66;
    // 88 is assigned to b
    b = 88;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "\nAfter a += b;" << endl;
    // assigning the sum of a and b to a
    a += b;  // a = a +b
    cout << "a = " << a << endl;
    return 0;
}
OUTPUT:
a = 66
b = 88
After a += b;
a = 154

Relational Operators in C++

To examine the connection between two operands, use a relational operator. For instance,

// checks if a is greater than b
a > b;

An example of a relational operator is >. It determines whether or not a is greater than b.

Returning 1 in the case of a true relationship and 0 in the case of a false relationship

OperatorNameExample
==Equal tox == y
!=Not equalx != y
Greater thanx > y
Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

EXAMPLE:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 6;
    b = 2;
    bool result;
//GeektoCode
    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;
    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;
    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;
    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;
    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;
    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}
OUTPUT:
3 == 5 is 0
3 != 5 is 1
3 > 5 is 1
3 < 5 is 0
3 >= 5 is 1
3 <= 5 is 0

Decision-making and loops both use relational operators.

logical operators in C++

Logical operators are used to determining whether an expression is true or false. When an expression is true, it gives a result of 1, while when it is false, it gives a result of 0.

OperatorExampleMeaning
&&expression1 && expression2Logical AND.
True only if all the operands are true.
||expression1 || expression2Logical OR.
True if at least one of the operands is true.
!!expressionLogical NOT.
True only if the operand is false.

Logical operators are frequently used in decision-making in C++. Let’s look at the following examples to help you better understand the logical operators:

EXAMPLE:

#include <iostream>
using namespace std;

int main() {
    bool result;
    result = (7 != 5) && (3 < 5);     // true
    cout << "(7 != 5) && (3 < 5) is " << result << endl;
    result = (7 == 5) && (3 < 5);    // false
    cout << "(7 == 5) && (3 < 5) is " << result << endl;
    result = (3 == 5) && (3 > 5);    // false
    cout << "(3 == 5) && (3 > 5) is " << result << endl;
    result = (7 != 5) || (3 < 5);    // true
    cout << "(7 != 5) || (3 < 5) is " << result << endl;
    result = (3 != 5) || (3 > 5);    // true
    cout << "(3 != 5) || (3 > 5) is " << result << endl;
    result = (2 == 5) || (3 > 5);    // false
    cout << "(2 == 5) || (3 > 5) is " << result << endl;
    result = !(5 == 2);    // true
    cout << "!(5 == 2) is " << result << endl;
    result = !(5 == 5);    // false
    cout << "!(5 == 5) is " << result << endl;
    return 0;
}
OUTPUT:
(7 != 5) && (3 < 5) is 1
(7 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(7 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(2 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0

Bitwise Operators in C++

Bitwise operators are used in C++ to carry out operations on specific bits. Only char and int data types can be used with them.

OperatorDescription
&Binary AND
|Binary OR
^Binary XOR
~Binary One’s Complement
<< Binary Shift Left
>> Binary Shift Right

EXAMPLE:

#include <iostream>
using namespace std;
//geektocode
int main() {
    int a = 44, b = 76;
    cout<< "a = " << a << endl;
    cout<< "b = " << b << endl;
    cout<< "a | b = " << (a | b) << endl;
    return 0;
}
OUTPUT:
a = 44
b = 76
a | b = 108

Other C++ Operators

Here is a list of some additional popular C++ operators. Later tutorials will cover them in detail.

OperatorDescriptionExample
sizeofreturns the size of data typesizeof(int); // 4
?:returns value based on the conditionstring result = (10 > 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;

Leave a Reply

Your email address will not be published. Required fields are marked *