Operators in C++
Operators are symbols in programming that allow us to do math or make decisions. They're like the tools we use to add numbers, compare values, or perform other tasks in code. In C++, there are special built-in symbols we use for different operations, making it easier to write programs.
Classification of Operators in C++
Operators in C++ are categorized into six main types, each serving specific purposes:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
Arithmetic Operators in C++
Arithmetic operators, as implied by their name, are employed for executing fundamental mathematical operations including addition, subtraction, multiplication, and division, among others. They are fundamental tools in C++ for performing numerical computations and manipulating numerical data.
Arithmetic operators in C++ are divided into two types: Unary and Binary, each fulfilling distinct roles in mathematical calculations.
Unary Operator
Unary arithmetic operator in C++ is used only with one operand to perform the arithmetic operations.
The increment operators are used to increment the value of a variable by 1, and decrement operators are used to decrement the value of a variable by 1.
Pre-increment or pre-decrement operators first manipulate the value and then get assigned to the variables, whereas post-increment or post-decrement operators first get assigned to the variable, and then the value gets manipulated.
The unary arithmetic operators are shown in the following table.
Sr. no | Name | Symbol | Description |
---|---|---|---|
1 | pre-increment | ++(operand) | value is first incremented and then assigned |
2 | post-increment | (operand)++ | value is first assigned and then incremented |
3 | pre-decrement | --(operand) | value is first decremented and then assigned |
4 | post-decrement | (operand)-- | value is first assigned and then decremented |
Code Example
Output
Binary Operator
The binary arithmetic operators are the ones that are used with 2 operands to perform the arithmetic operations.
Examples of binary arithmetic operators are shown in the following table.
Sr. no | Name | Symbol | Description |
---|---|---|---|
1 | addition | + | Used to get the addtion of two operands |
2 | subtraction | - | Used to get the subtraction of two operands |
3 | multiplication | * | Used to get the multiplication of two operands |
4 | division | / | Used to get the division of two operands |
5 | reminder(modulo) | % | Used to get the remainder of two operands when they are divided. |
Code Example
Output
Relational Operators in C++
Relational operators are mainly used to check for specific conditions. These are always binary operators because 2 operands are needed to evaluate the condition. If the condition is true, it will return 1, else if the condition is false, it will return 0.
The examples of different relational operators are given in the following table.
Operator | Meaning | Example |
---|---|---|
== | Equal to | 3 == 3 is true |
!= | Not equal to | 5 != 2 is true |
> | Greater than | 3 > 4 is false |
< | Less than | 9 < 2 is false |
>= | Greater than or equal to | 4 >= 4 is true |
<= | Less than or equal to | 2 <= 4 is true |
Code Example
Output
Logical Operators in C++
Logical operators are used to check logical conditions. They return the 1 when the result is true and 0 when the result is false.
There are three logical operators in c++:
- And operator in c++
- Or operator in c++
- Not operator in c++
These are as shown in the following table.
Operator | Meaning | Description | Expression |
---|---|---|---|
&& | Logical AND | And in C++ returns true only if all the operands are true or non-zero | (expression 1) && (expression 2) |
|| | Logical OR | Or in C++ returns true if either of the operands is true or non-zero | (expression 1) || (expression 2) |
! | Logical NOT | Not in C++ returns true if the operand is false or zero | !(expression) |
Code Example
Output
Bitwise Operators in C++
Bitwise operators are tools for manipulating individual bits in binary data, allowing for efficient low-level operations such as setting, clearing, and checking specific bits. They're crucial for tasks requiring precise control over binary representations of data and are commonly used in systems programming and optimization.
There are so many different bitwise operators which are used to work with bits. The list of bitwise operators is shown in the following table.
Operator | Meaning | Description |
---|---|---|
~ | One's complement | It flips binary digits, changing 1s to 0s and 0s to 1s. |
& | Binary AND | Performs the AND operation on each bit of both the operands |
| | Binary OR | Performs the OR operation on each bit of both the operands |
^ | Binary XOR | Performs the OR operation on each bit of both the operands |
<< | Shift left | Shifts each digit to left by the specified number of positions |
>> | Shift right | Shifts each digit to right by the specified number of positions |
Code Example
Output
Assignment Operators in C++
This operator in C++ is used to assign the value to the variable. These are always binary operators. For example: num = 6 will assign the value 6 to the variable num.
There are different versions of these assignment operators to combine with the arithmetic operations.
The list of commonly used assignment operators with their equivalent expressions is shown in the following table.
Operator | Example | Equivalent to | Description |
---|---|---|---|
= | a = b | a = b | The assignment operator (=) is responsible for assigning the value on its right side to the variable specified on its left side. |
+= | a += b | a = a + b | The compound addition assignment operator (+=) adds the value on the right to the current value of the variable on the left and assigns the result back to the variable on the left. |
-= | a -= b | a = a - b | The compound subtraction assignment operator (-=) subtracts the value on the right from the current value of the variable on the left, and then assigns the result back to the variable on the left. |
*= | a *= b | a = a * b | The compound multiplication assignment operator (*=) multiplies the current value of the variable on the left by the value on the right and assigns the result back to the variable on the left. |
/= | a /= b | a = a / b | The compound division assignment operator (/=) performs division by the value on the right side, and then assigns the result back to the variable on the left, updating its value accordingly. |
Code Example
Output
Ternary/Conditional Operators in C++
The ternary operator in c++, often denoted as "? :", is a concise way to express conditional logic in many programming languages. It takes three operands:
Here's how it works:
- The condition (Expression1) is evaluated first.
- If the condition is true, the value of Expression2 is returned.
- If the condition is false, the value of Expression3 is returned.
Code example
Output:
Other Common Operators in C++
Apart from the c++ operators who are listed above in this article, there are some other operators as well which are frequently used in C++. These operators are also important, and you will often see them in programs.
Operator | Example | Description |
---|---|---|
sizeof | sizeof(int); // 4 | It returns the size of datatype |
& | &num //address of num | Used to represent the memory address of variable |
. | player.score = 91; | Used to access the members like structure variables or class members |
-> | ptr->score = 32; | Used with the pointers to access the class or structure variables |
<< | cout << "Hello"; | Used to print the output on command line |
>> | cin >> num; | Used to take input from command line |
Code example:
Output:
C++ Operators Precedence Chart
There are so many operators in C++, and when they are used in the same expression, there is an order of precedence by which the operations get performed.
For eg., the * operator has the highest precedence than the + operator, so in the expression 8 * 2 + 3, the * operator gets first preference, and then +, so the answer comes as 19.
Associativity for operators in C++ is the direction from which the expression gets evaluated.
For eg. a = 5; Here 5 gets assigned in variable a, so the expression gets evaluated from right to left, so it is the associativity of the = operator.
The precedence order and the associativity of C++ operators are given in the following table. The operators are from top to bottom as per their precedence.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Conclusion
- Operators: We have seen what is operator in c++. Operators in C++ are symbols used to perform operations on values or variables.
- Types of Operators: They are classified into Arithmetic, Relational, Logical, Bitwise, Assignment, and Ternary operators.
- Arithmetic Operators: Used for basic arithmetic operations like addition, subtraction, multiplication, and division.
- Relational Operators: Check for specific conditions and return either true (1) or false (0).
- Logical Operators: Used to evaluate logical conditions and return true or false.
- Bitwise Operators: Manipulate individual bits in binary data, crucial for low-level operations.
- Assignment Operators: Assign values to variables, with compound versions for combined arithmetic operations.
- Ternary Operator: Offers a concise way to express conditional logic with three operands.