-
Hajipur, Bihar, 844101
Operators in C++ are special symbols used to perform operations on variables and values. They help manipulate data, perform calculations, compare results, and control program logic. In short, operators are the foundation of all computations in C++.
For example:
int sum = 10 + 5;
Here, + is an operator that adds two numbers.
Operators work with operands (the values they act upon). In the above example, 10 and 5 are operands, while + is the operator.
C++ provides a wide variety of operators grouped into several categories based on their function. The main types are:
Arithmetic Operators
Assignment Operators
Comparison (Relational) Operators
Logical Operators
Increment and Decrement Operators
Bitwise Operators
Conditional (Ternary) Operator
Sizeof and Comma Operators
Let’s go through each type in detail with examples.
Arithmetic operators are used to perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (Remainder) | a % b |
Example:
#include <iostream>
using namespace std;
int main() {
int a = 15, b = 4;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Remainder: " << a % b;
return 0;
}
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Remainder: 3
Assignment operators are used to assign values to variables.
| Operator | Example | Same As |
|---|---|---|
= |
x = 5 |
Assigns 5 to x |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 3 |
x = x % 3 |
Example:
int a = 10;
a += 5; // a = 15
a *= 2; // a = 30
Assignment operators are especially useful when you want to perform operations and assign results simultaneously.
These operators are used to compare two values and return a boolean result (true or false).
| Operator | Description | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Example:
int a = 10, b = 5;
cout << (a > b); // Returns 1 (true)
Relational operators are mainly used in conditions and loops to make decisions.
Logical operators combine multiple conditions. They are mostly used in if statements and loops.
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND | (a > 5 && b < 10) |
| ` | ` | |
! |
Logical NOT | !(a == b) |
Example:
int a = 7, b = 3;
if (a > 5 && b < 5) {
cout << "Both conditions are true";
}
These operators increase or decrease the value of a variable by 1.
| Operator | Description | Example |
|---|---|---|
++ |
Increment | ++x or x++ |
-- |
Decrement | --x or x-- |
Example:
int x = 10;
cout << ++x; // Pre-increment, x becomes 11 before printing
cout << x++; // Post-increment, prints 11 then x becomes 12
Bitwise operators work on bits rather than entire numbers. They are commonly used in low-level programming and embedded systems.
| Operator | Description | Example |
|---|---|---|
& |
AND | a & b |
| ` | ` | OR |
^ |
XOR | a ^ b |
~ |
NOT | ~a |
<< |
Left shift | a << 1 |
>> |
Right shift | a >> 1 |
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
cout << (a & b); // 1
The ternary operator ?: is a shorthand way of writing an if-else statement.
Syntax:
condition ? expression1 : expression2;
Example:
int age = 18;
string result = (age >= 18) ? "Eligible" : "Not Eligible";
cout << result;
Output:
Eligible
Used to determine the size of a data type or variable.
cout << sizeof(int); // Output: 4
Used to separate multiple expressions that are evaluated from left to right.
int a = (2, 4, 6);
cout << a; // Output: 6
The comma operator returns the value of the last expression.
When multiple operators appear in a single expression, precedence determines which operator is evaluated first, while associativity defines the direction of evaluation (left to right or right to left).
For example:
int result = 10 + 5 * 2; // result = 20, not 30
Here, multiplication has higher precedence than addition.
| Operator Type | Associativity |
|---|---|
Arithmetic (*, /, %) |
Left to Right |
Arithmetic (+, -) |
Left to Right |
Relational (<, >, <=, >=) |
Left to Right |
Logical (&&, ` |
|
Assignment (=, +=, -=) |
Right to Left |
Operators in C++ are essential tools for performing mathematical, logical, and comparison operations. From simple arithmetic to bitwise manipulations, they form the basis of every program. Understanding how each operator works — along with precedence and associativity — helps you write efficient and bug-free code. Mastering operators allows you to build logical expressions and implement real-world problem-solving easily in C++.
What are operators in C++ and how do they work with operands?
Write a C++ program to perform all arithmetic operations (addition, subtraction, multiplication, division, and modulus) on two integers.
What is the difference between assignment and comparison operators? Give examples of each.
Explain the working of logical operators in C++ with an example using if conditions.
Write a C++ program to take two numbers as input and display which one is greater using a relational operator.
What is the difference between pre-increment (++x) and post-increment (x++)? Show with a short code example.
Write a C++ program that uses the ternary operator to find the largest of two numbers.
What are bitwise operators? Write a program to demonstrate the use of &, |, and ^ operators.
Explain the concept of operator precedence and associativity with an example.
Write a program that uses both assignment and arithmetic operators to calculate the average of three numbers.