Basic C++ Operators
An operator is a special symbol used to perform operations on data. In C++, operators are categorized into several types based on their function and the number of operands used. Understanding operators is an important foundation in programming, because almost every program requires data operations.
C++ provides a wide variety of operators, from simple arithmetic operations to bit-level operations. Each operator has specific rules and a precedence that determines the evaluation order in an expression. Without a good understanding of operators, a programmer may encounter logic errors that are hard to detect.
Operator Categories in C++
The following are the main categories of operators in C++:
- Arithmetic Operators - Perform mathematical operations
- Relational Operators - Compare two values
- Logical Operators - Combine boolean conditions
- Bitwise Operators - Work at the bit level
- Assignment Operators - Assign a value to a variable
- Other Operators - sizeof, ternary, comma, and more
Code Example
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
// Arithmetic operators
cout << "=== Arithmetic Operators ===" << 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;
// Relational operators
cout << "\n=== Relational Operators ===" << 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;
cout << "a <= b : " << (a <= b) << endl;
// Logical operators
cout << "\n=== Logical Operators ===" << endl;
cout << "a && b : " << (a && b) << endl;
cout << "a || b : " << (a || b) << endl;
cout << "!a : " << (!a) << endl;
// Assignment operators
cout << "\n=== Assignment Operators ===" << endl;
int c = a;
cout << "c = a : " << c << endl;
c += b;
cout << "c += b : " << c << endl;
c -= b;
cout << "c -= b : " << c << endl;
c *= 2;
cout << "c *= 2 : " << c << endl;
// sizeof operator
cout << "\n=== Operator sizeof ===" << endl;
cout << "sizeof(int) : " << sizeof(int) << endl;
cout << "sizeof(double) : " << sizeof(double) << endl;
cout << "sizeof(char) : " << sizeof(char) << endl;
// Ternary operator
cout << "\n=== Operator Ternary ===" << endl;
int max = (a > b) ? a : b;
cout << "Largest value: " << max << endl;
return 0;
}
Code Explanation
- Arithmetic Operators (
+,-,*,/,%): Produce a numeric value from a mathematical operation. The%(modulus) operator produces the remainder of a division. - Relational Operators (
==,!=,>,<,>=,<=): Produce a boolean valuetrue(1) orfalse(0) based on comparing two values. - Logical Operators (
&&,||,!): Combine or negate boolean conditions.&&requires both conditions to be true;||requires only one to be true. - Assignment Operators (
=,+=,-=,*=): Assign a value to a variable. Compound assignment operators make it easier to perform an assignment together with another operation. - sizeof Operator: Returns the size in bytes of a data type or variable.
- Ternary Operator (
?:): A short conditional operator that replaces a simpleif-else.
Operator Precedence Table
Operator precedence determines the evaluation order in an expression. Operators with higher precedence are evaluated first.
| Precedence | Operator | Description |
|---|---|---|
| 1 (Highest) | :: |
Scope resolution |
| 2 | () [] -> . |
Parentheses, array access, member access |
| 3 | ! ~ ++ -- (type) |
Unary operators |
| 4 | * / % |
Multiplicative |
| 5 | + - |
Additive |
| 6 | << >> |
Bitwise shift |
| 7 | < <= > >= |
Relational |
| 8 | == != |
Equality |
| 9 | & |
Bitwise AND |
| 10 | ^ |
Bitwise XOR |
| 11 | \| |
Bitwise OR |
| 12 | && |
Logical AND |
| 13 | \|\| |
Logical OR |
| 14 | ?: |
Ternary |
| 15 | = += -= ... |
Assignment |
| 16 (Lowest) | , |
Comma |
Everyday Use Cases
Basic operators are used everywhere in everyday life. Here is a simple example:
#include <iostream>
using namespace std;
int main() {
// Calculating the shopping price
int productPrice = 15000;
int quantity = 3;
double discount = 0.1;
int totalBeforeDiscount = productPrice * quantity;
int totalAfterDiscount = totalBeforeDiscount - (totalBeforeDiscount * discount);
cout << "Product price : $" << productPrice << endl;
cout << "Quantity : " << quantity << " pcs" << endl;
cout << "Total (before discount): $" << totalBeforeDiscount << endl;
cout << "Discount 10% : $" << (totalBeforeDiscount * discount) << endl;
cout << "Total (after discount): $" << totalAfterDiscount << endl;
// Checking whether the money is enough
int myMoney = 40000;
bool enough = (myMoney >= totalAfterDiscount);
cout << "\nMy money : $" << myMoney << endl;
cout << "Enough? : " << (enough ? "Yes" : "No") << endl;
return 0;
}
Conclusion
Basic operators are the fundamental building blocks in C++. There are six main categories of operators: arithmetic, relational, logical, assignment, bitwise, and others. Operator precedence determines the evaluation order in an expression. A good understanding of operators and their precedence is essential for writing correct and efficient code.