In C++, operators are symbols that perform operations on variables and values. They are essential for performing calculations, comparisons, and logical evaluations in your programs. Understanding operators is crucial as they form the backbone of any programming language.
Operators in C++ can be broadly categorized into several types: arithmetic, assignment, comparison, logical, bitwise, ternary, sizeof, comma, and scope resolution. Each type serves a specific purpose and has its own set of rules and precedence levels. In this tutorial, we will explore each type of operator with examples to help you understand their usage and importance.
Arithmetic operators are used for performing basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 3;6cout << "Addition: " << a + b << endl;7cout << "Subtraction: " << a - b << endl;8cout << "Multiplication: " << a * b << endl;9cout << "Division: " << a / b << endl;10cout << "Modulus: " << a % b << endl;11return 0;12}
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1
Assignment operators are used to assign values to variables. The basic assignment operator is =.
| Operator | Description | Equivalent To |
|---|---|---|
= | Simple assignment | a = b |
+= | Addition assignment | a = a + b |
-= | Subtraction assignment | a = a - b |
*= | Multiplication assignment | a = a * b |
/= | Division assignment | a = a / b |
%= | Modulus assignment | a = a % b |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 5;6a += b; // Equivalent to: a = a + b7cout << "a after += : " << a << endl;89a -= b; // Equivalent to: a = a - b10cout << "a after -= : " << a << endl;1112a *= b; // Equivalent to: a = a * b13cout << "a after *= : " << a << endl;1415a /= b; // Equivalent to: a = a / b16cout << "a after /= : " << a << endl;1718a %= b; // Equivalent to: a = a % b19cout << "a after %= : " << a << endl;2021return 0;22}
a after += : 15 a after -= : 10 a after *= : 50 a after /= : 10 a after %= : 0
Comparison operators are used to compare two values. They 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 |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 5;67if (a == b) cout << "a is equal to b" << endl;8else cout << "a is not equal to b" << endl;910if (a > b) cout << "a is greater than b" << endl;11else cout << "a is not greater than b" << endl;1213return 0;14}
a is not equal to b a is greater than b
Logical operators are used to combine conditional statements. They include AND, OR, and NOT operations.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 5;67if (a > 0 && b > 0) cout << "Both a and b are positive" << endl;8else cout << "At least one of a or b is non-positive" << endl;910if (a > 0 || b > 0) cout << "At least one of a or b is positive" << endl;11else cout << "Neither a nor b is positive" << endl;1213if (!a) cout << "a is zero" << endl;14else cout << "a is not zero" << endl;1516return 0;17}
Both a and b are positive At least one of a or b is positive a is not zero
Bitwise operators perform operations on individual bits of numbers. They include AND, OR, XOR, NOT, left shift, and right shift.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 1 |
>> | Right shift | a >> 1 |
1#include <iostream>2using namespace std;34int main() {5int a = 5, b = 3; // Binary: a = 0101, b = 001167cout << "Bitwise AND: " << (a & b) << endl; // 0001 -> 18cout << "Bitwise OR: " << (a | b) << endl; // 0111 -> 79cout << "Bitwise XOR: " << (a ^ b) << endl; // 0110 -> 610cout << "Bitwise NOT of a: " << (~a) << endl; // 1010 -> -6 (two's complement)1112cout << "Left shift of a by 1: " << (a << 1) << endl; // 1010 -> 1013cout << "Right shift of a by 1: " << (a >> 1) << endl; // 0010 -> 21415return 0;16}
Bitwise AND: 1 Bitwise OR: 7 Bitwise XOR: 6 Bitwise NOT of a: -6 Left shift of a by 1: 10 Right shift of a by 1: 2
The ternary operator is a shorthand for the if-else statement. It takes three operands.
| Syntax | Description |
|---|---|
condition ? expression1 : expression2 | If condition is true, evaluate expression1; otherwise, evaluate expression2 |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 5;67int max = (a > b) ? a : b;8cout << "Max value: " << max << endl;910return 0;11}
Max value: 10
The sizeof operator returns the size, in bytes, of its operand.
| Syntax | Description |
|---|---|
sizeof(type) | Returns the size of the specified type |
1#include <iostream>2using namespace std;34int main() {5cout << "Size of int: " << sizeof(int) << " bytes" << endl;6cout << "Size of char: " << sizeof(char) << " byte" << endl;7cout << "Size of double: " << sizeof(double) << " bytes" << endl;89return 0;10}
Size of int: 4 bytes Size of char: 1 byte Size of double: 8 bytes
The comma operator evaluates its first operand and discards the result, then evaluates and returns the second operand.
| Syntax | Description |
|---|---|
expression1, expression2 | Evaluates both expressions; returns the value of expression2 |
1#include <iostream>2using namespace std;34int main() {5int a = 5, b;67b = (a++, a + 3); // First, a is incremented to 6, then a + 3 is evaluated as 98cout << "b: " << b << endl; // Output: 9910return 0;11}
b: 9
The scope resolution operator :: is used to access members of a namespace or class.
| Syntax | Description |
|---|---|
namespace::identifier | Accesses identifier in the specified namespace |
1#include <iostream>2using namespace std;34namespace MyNamespace {5int value = 10;6}78int main() {9cout << "Value from namespace: " << MyNamespace::value << endl;1011return 0;12}
Value from namespace: 10
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.
| Precedence | Operator(s) |
|---|---|
| Highest | () |
[] | |
-> | |
:: | |
++, -- (postfix) | |
!, ~, +, - (unary) | |
*, /, % | |
+, - | |
<<, >> | |
<, <=, >, >= | |
==, != | |
& | |
| | |
^ | |
&& | |
|| | |
?: | |
= (and other assignment operators) | |
| Lowest | , |
1#include <iostream>2using namespace std;34int main() {5int a = 10, b = 5, c = 2;67int result = a + b * c; // Multiplication has higher precedence than addition8cout << "Result: " << result << endl; // Output: 20910return 0;11}
Result: 20
Let's create a simple program that calculates the area of a rectangle using user input and demonstrates the use of various operators.
1#include <iostream>2using namespace std;34int main() {5double length, width;67cout << "Enter the length of the rectangle: ";8cin >> length;910cout << "Enter the width of the rectangle: ";11cin >> width;1213double area = length * width;14cout << "The area of the rectangle is: " << area << endl;1516return 0;17}
| Operator Type | Description |
|---|---|
| Arithmetic | Basic math operations |
| Assignment | Assign values to variables |
| Comparison | Compare two values |
| Logical | Combine conditional statements |
| Bitwise | Perform bitwise operations |
| Ternary | Shorthand for if-else |
| sizeof | Get size of data types |
| Comma | Evaluate multiple expressions |
| Scope Resolution | Access namespace or class members |
In the next tutorial, we will explore Booleans and how they are used in C++ to represent true/false conditions. Understanding booleans is essential for controlling program flow with conditional statements.
Stay tuned!