Operators in C++
1. What is an Operator?
An operator is a symbol that tells the compiler to perform a specific operation on one or more values or variables.
For example:
int a = 10; int b = 5; int sum = a + b;
Here, + is an operator that performs addition.
The values or variables on which an operator works are called operands.
a + b ↑ ↑ Operand
2. Types of Operators in C++
C++ provides several types of operators:
Operators │ ├── Arithmetic Operators ├── Relational Operators ├── Logical Operators ├── Assignment Operators ├── Increment/Decrement Operators ├── Bitwise Operators ├── Conditional Operator ├── sizeof Operator ├── Type Casting Operators └── Other/Special Operators
3. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus | 10 % 3 | 1 |
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 3; cout << "Addition: " << a + b << endl; cout << "Subtraction: " << a - b << endl; cout << "Multiplication: " << a * b << endl; cout << "Division: " << a / b << endl; cout << "Remainder: " << a % b << endl; return 0; }
Output:
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Remainder: 1
Important
When both operands are integers, / performs integer division:
10 / 3
produces:
3
To obtain a decimal result:
10.0 / 3
produces approximately:
3.33333
4. Relational Operators
Relational operators compare two values.
The result is a Boolean value: true or false.
| Operator | Meaning | 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
#include <iostream> using namespace std; int main() { int a = 10; int b = 5; cout << (a == b) << endl; cout << (a != b) << endl; cout << (a > b) << endl; cout << (a < b) << endl; return 0; }
Output:
0 1 1 0
You can use boolalpha to display true and false:
cout << boolalpha << (a > b);
Output:
true
5. Logical Operators
Logical operators are used to combine or reverse Boolean conditions.
| Operator | Name | Example | ||||
|---|---|---|---|---|---|---|
&& | Logical AND | a > 5 && b < 10 | ||||
| ` | ` | Logical OR | `a > 5 | b > 10` | ||
! | Logical NOT | !(a > 5) |
Logical AND (&&)
Returns true only when both conditions are true.
int age = 20; cout << (age >= 18 && age <= 60);
Output:
1
Logical OR (||)
Returns true when at least one condition is true.
int age = 15; cout << (age < 18 || age > 60);
Output:
1
Logical NOT (!)
Reverses a Boolean value.
bool passed = true; cout << !passed;
Output:
0
6. Assignment Operators
Assignment operators are used to assign or update values.
| Operator | Example | Equivalent |
|---|---|---|
= | a = 10 | Assign 10 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
Example
#include <iostream> using namespace std; int main() { int a = 10; a += 5; cout << a << endl; a -= 3; cout << a << endl; a *= 2; cout << a << endl; return 0; }
Output:
15 12 24
7. Increment and Decrement Operators
These operators increase or decrease a variable by 1.
| Operator | Meaning |
|---|---|
++ | Increment by 1 |
-- | Decrement by 1 |
Increment
int x = 10; x++;
Now x is 11.
Decrement
int x = 10; x--;
Now x is 9.
8. Prefix and Postfix
Increment and decrement operators can be used in two forms.
Prefix
The variable is changed before its value is used.
int x = 5; cout << ++x;
Output:
6
Postfix
The current value is used first, then the variable is changed.
int x = 5; cout << x++;
Output:
5
After the statement, x becomes 6.
9. Bitwise Operators
Bitwise operators work on the individual bits of integral or enumeration values.
| Operator | Name | |
|---|---|---|
& | Bitwise AND | |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | |
~ | Bitwise NOT | |
<< | Left shift | |
>> | Right shift |
Example
#include <iostream> using namespace std; int main() { int a = 5; int b = 3; cout << (a & b) << endl; cout << (a | b) << endl; cout << (a ^ b) << endl; return 0; }
Binary representation:
5 = 0101 3 = 0011
Bitwise AND:
0101 0011 ---- 0001 = 1
Bitwise OR:
0101 0011 ---- 0111 = 7
Bitwise XOR:
0101 0011 ---- 0110 = 6
10. Conditional Operator
The conditional operator is also called the ternary operator.
It uses three operands.
Syntax
condition ? value_if_true : value_if_false;
Example:
int age = 20; string result = (age >= 18) ? "Adult" : "Minor"; cout << result;
Output:
Adult
It is often used as a short alternative to a simple if-else.
11. sizeof Operator
The sizeof operator determines the size, in bytes, of a type or object.
Example:
#include <iostream> using namespace std; int main() { int x; cout << sizeof(x); return 0; }
On many modern systems, the output is:
4
The exact size can depend on the implementation and platform.
You can also write:
cout << sizeof(int); cout << sizeof(double); cout << sizeof(char);
12. Address-of Operator (&)
The & operator can be used to obtain the memory address of an object.
Example:
int age = 20; cout << &age;
This prints the memory address of age.
It is commonly used with pointers.
int age = 20; int* ptr = &age;
Here, ptr stores the address of age.
13. Dereference Operator (*)
The * operator can be used to access the value stored at the address held by a pointer.
Example:
int age = 20; int* ptr = &age; cout << *ptr;
Output:
20
Here:
&age→ address ofageptr→ stores that address*ptr→ value stored at that address
14. Scope Resolution Operator (::)
The scope resolution operator is used to access names belonging to a particular scope.
Example:
#include <iostream> int value = 100; int main() { int value = 50; std::cout << value << std::endl; std::cout << ::value << std::endl; return 0; }
Output:
50 100
Here, ::value accesses the global variable.
It is also widely used with classes and namespaces.
15. Member Access Operators
The dot (.) operator is used to access members of an object.
Example:
struct Student { int age; }; int main() { Student s; s.age = 20; }
For a pointer to an object, the arrow (->) operator is commonly used:
Student* ptr = &s; ptr->age = 20;
16. Comma Operator
The comma operator allows multiple expressions to be evaluated from left to right.
Example:
int a, b; a = 10, b = 20;
Now:
a = 10 b = 20
17. new and delete Operators
C++ provides new and delete for dynamic memory management.
new
Allocates memory dynamically.
int* ptr = new int; *ptr = 25;
delete
Releases dynamically allocated memory.
delete ptr;
For arrays:
int* arr = new int[5]; delete[] arr;
In modern C++, smart pointers are generally preferred for managing dynamically allocated objects.
18. Type Casting Operators
Type casting converts a value from one type to another.
Modern C++ provides several explicit cast operators, including:
static_castdynamic_castconst_castreinterpret_cast
Example using static_cast:
double price = 99.99; int value = static_cast<int>(price); cout << value;
Output:
99
19. Operator Precedence
When an expression contains multiple operators, C++ follows rules called operator precedence to determine which operation is performed first.
Example:
int result = 10 + 5 * 2;
Multiplication is performed before addition:
5 * 2 = 10 10 + 10 = 20
Therefore:
result = 20
Parentheses can be used to control the order:
int result = (10 + 5) * 2;
Now:
15 * 2 = 30
So:
result = 30
20. Operator Precedence Summary
Some commonly used operators, from higher to lower precedence, include:
| Level | Operators | Description | ||
|---|---|---|---|---|
| High | (), [], . | Function call, indexing, member access | ||
++, -- | Increment/decrement | |||
*, /, % | Multiplication, division, modulus | |||
+, - | Addition, subtraction | |||
<, <=, >, >= | Relational | |||
==, != | Equality | |||
&& | Logical AND | |||
| ` | ` | Logical OR | ||
| Low | =, +=, -=, etc. | Assignment |
When in doubt, use parentheses to make the intended order clear.
21. Example Program Using Operators
#include <iostream> using namespace std; int main() { int a, b; cout << "Enter two numbers: "; cin >> a >> b; cout << "Addition: " << a + b << endl; cout << "Subtraction: " << a - b << endl; cout << "Multiplication: " << a * b << endl; if (b != 0) { cout << "Division: " << static_cast<double>(a) / b << endl; cout << "Remainder: " << a % b << endl; } cout << "a > b: " << boolalpha << (a > b) << endl; cout << "a == b: " << (a == b) << endl; return 0; }
Example input:
Enter two numbers: 10 3
Output:
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.33333 Remainder: 1 a > b: true a == b: false
22. Operator Summary
| Category | Operators | ||
|---|---|---|---|
| Arithmetic | +, -, *, /, % | ||
| Relational | ==, !=, >, <, >=, <= | ||
| Logical | &&, ` | , !` | |
| Assignment | =, +=, -=, *=, /=, %= | ||
| Increment/Decrement | ++, -- | ||
| Bitwise | &, ` | , ^, ~, <<, >>` | |
| Conditional | ?: | ||
| Size | sizeof | ||
| Address | & | ||
| Dereference | * | ||
| Scope Resolution | :: | ||
| Member Access | ., -> | ||
| Dynamic Memory | new, delete | ||
| Comma | , |
23. Important Points
- Operators perform operations on values and variables.
- Arithmetic operators perform mathematical calculations.
- Relational operators compare values.
- Logical operators combine conditions.
- Assignment operators assign or update values.
++and--increase or decrease values by one.- Bitwise operators work with individual bits.
- The conditional operator
?:provides a compact conditional expression. sizeofdetermines the size of an object or type in bytes.&can obtain an object's address, while*can dereference a pointer.- Operator precedence determines the order in which parts of an expression are evaluated.
Conclusion
Operators are essential building blocks of C++ expressions. They allow programmers to perform calculations, compare values, make logical decisions, assign data, manipulate bits, work with memory, and perform many other operations. Understanding operators is important before learning control flow, functions, arrays, pointers, and object-oriented programming.