Operators in C++ are symbols that help the compiler to perform various types of mathematical, logical, and bitwise operations on the operands. Also, operators help developers to manipulate data and do computations easily. In this article, we will discuss what operators are in C++, types of operators, miscellaneous operators, and operator precedence and associativity in C++.
Table of Contents:
What are Operators in C++?
Operators in C++ are the symbols that perform operations on the variables and values, and they are used to manipulate data, execute computations, and control program flow. C++ provides various types of operators, which help to perform different programming operations efficiently.
Types of Operators in C++
Operators in C++ are of six types based on the operations they perform. Now, let’s discuss each in detail.
1. Arithmetic Operators
Arithmetic operators in C++ are used to perform the basic mathematical operations such as addition, multiplication, division, etc. These operators are classified into two types, as Unary and Binary operators.
- Unary operators are the operators that perform operations on a single operand.
- Binary operators are the operators that perform operations on two operands.
Here is the description of all the arithmetic operators in C++:
Type |
Operator Name |
Symbol |
Description |
Binary | Addition | + | Adds two values. |
| Subtraction | – | Subtracts the second value from the first. |
| Multiplication | * | Multiplies two values. |
| Division | / | Divides the first value by the second. |
| Modulus | % | Returns the remainder of the division. |
Unary | Unary Plus | + | Indicates a positive number. |
| Unary Minus | – | Negates the value. |
| Increment | ++ | Increases value by 1. |
| Decrement | — | Decreases value by 1. |
Example:
Output:
The code shows the arithmetic operations in a C++ program along with pre-increment (++x) and post-increment(x++), which return the value before incrementing, using the arithmetic operators to show how the value changes after the operations.
2. Relational Operators
Relational operators in C++ are used to compare two values and return a Boolean value, either true(1) or false(0), as a result of the comparison. These operators are also known as the Comparison Operators.
Here is the description of all the relational operators in C++:
Operator Name |
Symbol |
Description |
Equal to | == | It returns true if both values are equal. |
Not equal to | != | It returns true if the values are not equal. |
Greater than | > | It returns true if the left value is greater. |
Less than | < | It returns true if the left value is smaller. |
Greater than or equal to | >= | It returns true if the left value is greater than or equal to. |
Less than or equal to | <= | It returns true if the left value is smaller or equal. |
Example:
Output:
The code shows how the relational operators give the results by comparing two integers, a and b, and then prints 1 as true and 0 as false based on the evaluation of equality conditions.
3. Logical Operators
Logical Operators in C++ are used to perform logical operations on Boolean expressions and return true or false as a result by evaluating the given conditions. They are commonly used in decision-making statements such as if, while, and for loops.
Here is the description of all the logical operators in C++:
Operator Name |
Symbol |
Description |
Logical AND | && | Returns true if both conditions are true. |
Logical OR | || | Returns true if at least one condition is true. |
Logical NOT | ! | Reverses the logical value of the condition. |
Example:
Output:
The code shows how the logical operators give the results by checking that the two integers a and b are true or not for the conditions provided in the program and prints the output according to the evaluations.
4. Bitwise Operators
Bitwise operators in C++ are used to perform operations at the bit level, which manipulate the individual bits of integer data types. The operands are first changed into bits, such as 0 and 1, and then the evaluation is done.
Here is the description of all the bitwise operators in C++:
Operator Name |
Symbol |
Description |
Bitwise AND | & | It sets each bit to 1 if both corresponding bits are 1. |
Bitwise OR | | | It sets each bit to 1 if at least one corresponding bit is 1. |
Bitwise XOR | ^ | It sets each bit to 1 if the corresponding bits are different. |
Bitwise NOT | ~ | It inverts all bits, such as 1s become 0s and vice versa. |
Left Shift | << | It shifts bits to the left, effectively multiplying by 2 per shift. |
Right Shift | >> | It shifts bits to the right, effectively dividing by 2 per shift. |
Example:
Output:
The code shows how the bitwise operators on two integers, a and b, perform various bitwise operations and prints the results based on the binary calculations.
5. Assignment Operators
Assignment operators in C++ are the operators that are used to assign a value to the variables. These operators also perform arithmetic or bitwise operations before assigning the result.
Here is the description of all the assignment operators in C++:
Operator Name |
Symbol |
Description |
Assignment | = | Assigns a value. |
Add and Assign | += | Adds and assigns. |
Subtract and Assign | -= | Subtracts and assigns. |
Multiply and Assign | *= | Multiplies and assigns. |
Divide and Assign | /= | Divides and assigns. |
Modulus and Assign | %= | Computes modulus and assigns. |
Bitwise AND and Assign | &= | Bitwise AND and assigns. |
Bitwise OR and Assign | |= | Bitwise OR and assigns. |
Bitwise XOR and Assign | ^= | Bitwise XOR and assignments. |
Left Shift and Assign | <<= | Left shifts and assigns. |
Right Shift and Assign | >>= | Right shifts and assigns. |
Example:
Output:
The code shows how the assignment operators perform arithmetic operations with two integers, a and b, then updates the results and prints the results step by step to the console.
6. Ternary (Conditional) Operator
The ternary operator in C++ is an alternative to if-else statements, which evaluates if a condition is true or false and returns one of two values based on that condition. This operator is also known as the Conditional operator in C++. Also, the ternary operator works on three operands.
Syntax:
condition? expression1 : expression2;
Returns:
- If the condition is true, then the expression1 is executed.
- If the condition is false, then the expression2 is executed.
Example:
Output:
The code shows how the ternary operator compares two integers, a and b, and then assigns the larger value to the max, which is then printed as a result to the console.
Miscellaneous Operators in C++
There are a few miscellaneous operators in C++ that are not used for any type of operations and cannot be classified into any of the above-discussed categories.
Operator Name |
Symbol |
Description |
Sizeof Operator | sizeof | Returns the size (in bytes) of a data type or variable. |
Comma Operator | , | Evaluates multiple expressions, returning the last one. |
Address-of Operator | & | Returns the memory address of a variable. |
Dot Operator | . | Accesses members of an object. |
Arrow Operator | -> | Accesses members of an object through a pointer. |
Casting Operators | static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<> | Convert one data type to another. |
Let’s know each one of them in more detail with C++ examples:
1. Sizeof Operator (sizeof)
The sizeof operator in C++ gives the size of a data type or a variable in bytes as a result, and it is a compile-time operator that is used to know the memory requirements of a data type used in the program.
Syntax:
sizeof(type) // For data types
sizeof(variable) // For variables
Example:
Output:
The code shows how the sizeof operator returns the memory size in bytes of the integer x and double y, which is then printed as a result to the console.
2. Comma Operator (,)
The comma operator in C++ evaluates multiple expressions from left to right and returns the value of the last expression as a result.
Syntax:
expression1, expression2, ..., expressionN
Example:
Output:
The code shows how the comma operator evaluates multiple expressions in a = (5, 10); and z = (x += 5, y += 10, x + y); and then after evaluating both the expressions, a is assigned the value 10 and z is assigned with 18, which is printed to the console.
3. Address-of Operator (&)
The address-of operator in C++ gives the memory address of a variable as a result. It is commonly used with pointers to store and manipulate the memory locations.
Example:
Output:
The code shows how the address-of operator gives the memory address as a result of the initialized variable x wth the value of 10, and the ptr helps to access the memory location of the variable as it stores the address of x.
4. Dot Operator (.)
The dot operator in C++ is used to access the members of a class, structure, or union using an object in the program.
Example:
Output:
The code shows that the dot operator is used to access the members s1.name and s1.age of a struct Student, and then the values are printed to the console.
5. Arrow Operator (->)
The arrow operator in C++ is used to access the members of a structure or a class using a pointer to an object in the program.
Example:
Output:
The code shows how the arrow operator is used to access the members of the class Car using a pointer, the ptr->show() calls the function, and the ptr-> brand accesses the brand variable of the Car object, and then the result is printed to the console.
6. Casting Operators
Casting operators in C++ are used to convert one data type to another. There are four types of casting operators: static_cast<>, dynamic_cast<>, const_cast<>, and reinterpret_cast<>.
Example:
Output:
The code shows how the static_cast<> operator converts a double into an int by truncating the decimal part of the value 3.14 and storing 3 in the intNum, and then the result is printed to the console.
Operator Precedence and Associativity in C++
1. Operator Precedence
The operator precedence checks the order in which operators are evaluated in an expression, and the operators with higher precedence are evaluated first.
2. Operator Associativity
When multiple operators of the same precedence appear in an expression, then the associativity defines the order of evaluation. The order of precedence can be:
- Left to Right (e.g., +, -, *, /)
- Right to Left (e.g., =, +=, -=, ?:)
Category |
Operators |
Associativity |
Scope Resolution | :: | Left to Right |
Function, Array, Member Access | () [] ->. | Left to Right |
Unary Operators | ++ — + – ! ~ (type) * & sizeof | Right to Left |
Multiplicative | * / % | Left to Right |
Additive | + – | Left to Right |
Bitwise 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 |
Ternary/Conditional | ?: | Right to Left |
Assignment | = += -= *= /= %= <<= >>= &= ^= |= | Right to Left |
Comma Operator | , | Left to Right |
Conclusion
Operators in C++ are important symbols that help developers to manipulate data, execute computations, and control the flow of the program. Also, these operators are classified into different types in C++ based on their operations. Operators also have precedence and associativity. So, understanding what operators are and how they work is very important to perform operations in C++ programming efficiently.
FAQs on Operators in C++
Q1. What are operators in C++?
Operators in C++ are the symbols that perform operations on the variables and values.
Q2. How many types of operators are there in C++?
There are six types of operators in C++ based on the operations they perform.
Q3. What is the difference between unary and binary operators?
The difference between a unary operator and a binary operator is that unary operators operate on a single operand, and binary operators operate on two operands.
Q4. How do bitwise operators work in C++?
Bitwise operators in C++ manipulate individual bits of integer data.
Q5. How does the sizeof operator work?
The sizeof operator returns the memory size in bytes of a data type or variable.