C++ Operators
In C++, operators are special symbols or keywords used to perform operations on variables and values. Operators allow you to manipulate data and variables in your program, whether it's performing mathematical calculations, comparisons, or logical operations. C++ has a wide variety of operators that can be grouped into different categories, each serving a unique purpose.
In this post, we’ll discuss the following categories of operators in C++:
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder of division)
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;
return 0;
}
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Relational operators are used to compare two values and determine the relationship between them. These operators return a boolean value (true
or false
).
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
Logical operators are used to perform logical operations, typically with boolean values (true or false).
&&
: Logical AND||
: Logical OR!
: Logical NOT
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
cout << "(a && b): " << (a && b) << endl; // AND
cout << "(a || b): " << (a || b) << endl; // OR
cout << "!(a && b): " << !(a && b) << endl; // NOT
return 0;
}
Output:
(a && b): 0
(a || b): 1
!(a && b): 1
Bitwise operators operate on the binary representation of numbers. These operators are useful for manipulating individual bits of data.
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT<<
: Left shift>>
: Right shift
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout << "a & b: " << (a & b) << endl; // AND
cout << "a | b: " << (a | b) << endl; // OR
cout << "a ^ b: " << (a ^ b) << endl; // XOR
cout << "~a: " << (~a) << endl; // NOT
cout << "a << 1: " << (a << 1) << endl; // Left shift
cout << "a >> 1: " << (a >> 1) << endl; // Right shift
return 0;
}
Output:
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
Assignment operators are used to assign values to variables. C++ provides several types of assignment operators.
=
: Simple assignment+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign
#include <iostream>
using namespace std;
int main() {
int a = 10;
a += 5; // Equivalent to a = a + 5
cout << "a after += 5: " << a << endl;
a *= 2; // Equivalent to a = a * 2
cout << "a after *= 2: " << a << endl;
return 0;
}
Output:
a after += 5: 15
a after *= 2: 30
The increment (++
) and decrement (--
) operators are used to increase or decrease a variable's value by 1, respectively.
++a
: Pre-increment (increase value before using it)a++
: Post-increment (increase value after using it)--a
: Pre-decrement (decrease value before using it)a--
: Post-decrement (decrease value after using it)
#include <iostream>
using namespace std;
int main() {
int a = 10;
cout << "Pre-increment: " << ++a << endl; // Increments a and then displays
cout << "Post-increment: " << a++ << endl; // Displays a and then increments
cout << "After post-increment: " << a << endl;
return 0;
}
Output:
Pre-increment: 11
Post-increment: 11
After post-increment: 12
The conditional operator (?:
) is a shorthand for an if-else statement. It is used to evaluate expressions based on a condition.
condition ? expression_if_true : expression_if_false;
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b; // Assigns the larger value to 'max'
cout << "Maximum value: " << max << endl;
return 0;
}
Output:
Maximum value: 10
sizeof
: Used to determine the size (in bytes) of a variable or data type.sizeof
and Type-casting:
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << "Size of x: " << sizeof(x) << " bytes" << endl;
double pi = 3.14;
cout << "Type-casting: " << (int)pi << endl; // Convert double to int
return 0;
}
Output:
Size of x: 4 bytes
Type-casting: 3