C++ supports a rich set of built-in operators, we have already used several of them like =, &, etc. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. operators are used in the program to manipulate data and variables. They usually form mathematical or logical expressions.
Operators in C++ can be classified into following groups:
- Arithmetic operators
- Assignment operator
- Compound assignment
- Logical operators
- Bitwise operators
- Increment and decrement operators
- Conditional ternary operator
- sizeof()
- Other Operators
Arithmetic operators
C++ provides all the basic arithmetic operators. They all work the same as they do in other languages. These can operate on any built-in data type allowed in C++. They are:-
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Divide (/)
- Modulo (%)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> using namespace std; int main() { int a, b; a = 7; b = 2; // printing the sum of a and b cout << "a + b = " << (a + b) << endl; // printing the difference of a and b cout << "a - b = " << (a - b) << endl; // printing the product of a and b cout << "a * b = " << (a * b) << endl; // printing the division of a by b cout << "a / b = " << (a / b) << endl; // printing the modulo of a by b cout << "a % b = " << (a % b) << endl; return 0; } |
Output:
1 2 3 4 5 |
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a % b = 1 |
Assignment operator (=)
The assignment operator assigns the right side variable value to the left variable.
a=7;
This statement assigns the integer value 7 to the variable a. The assignment operation always takes place from right to left
This statement assigns to variable the value contained in variable b. The value of a at the moment this statement is executed is lost and replaced by the value of b.
Compound assignment
Compound assignment operators modify the current value by performing an operation.
- + =
- – =
- * =
- / =
Logical operators
C++ provides three logical operators when we test more than one condition and make decisions.
These are:
- && (meaning logical AND)
- || (meaning logical OR)
- ! (meaning logical NOT)
Bitwise Operators
Bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types
Operator | Description |
---|---|
& |
Binary AND |
| |
Binary OR |
^ |
Binary XOR |
~ |
Binary One’s Complement |
<< |
Binary Shift Left |
>> |
Binary Shift Right |
Increment and decrement operators (++, –)
C++ allows two very useful operators not generally found in other languages. These are the increment and decrement operators.
The operator ++ adds 1 to the operand, while — subtract 1. Both are unary operators and takes the following form.
|
++i or i++ and —i or i—
|
We use the increment and decrement statement in the loop extensively.
While ++i and i++ mean the same thing they are form statements independently, they behave differently;y when they are used in the expression on the right-hand side of an assignment statement. Consider the following:-
i=1;
j=++i;
In this case, the value of j and I would be 2. Suppose if we rewrite the above statement as:-
i=1;
j=i++;
then, the value of j would be 1 and i would be 2. A prefix operator first adds 1 to the operand and then the result assigned to the variable on left. on the other hand, a postfix operator first assigns and then increments the operands.
Conditional ternary operator
A ternary operator pair ” ? :” is available in C++ to construct conditional expression form:-
condition ? result_1 : result_2
If the condition is true, the entire expression evaluates to result_1, and otherwise to result_2.
sizeof() operator
The Sizeof is a compile-time operator and when used with an operand, it returns the number of bytes the operation occupies. The operation may be a variable, constants, or a data type qualifier. For example:-
1 |
sizeof(int) |
It returns the size of int
Other Operators
- & Returns the address of a memory location.
- * Pointer to a variable.