C++ Operators
Last Updated : 10 Jan 2026
In C++, operators are special symbols that are used to perform operations on variables and values on operands. Operators are essential concepts of any programming language. There can be several operations, including arithmetic, value assignment, bitwise, and logical computations.
C++ Operators Example
Let us take a basic example to illustrate operators in C++.
Example
Output:
c: 50
Types of Operators
There are following types of operators to perform different types of operations in C++ language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
- Misc Operators
Here, we will discuss these C++ operators one by one with their types and examples.
1. Arithmetic Operators:
In C++, arithmetic operations are mainly utilized to perform mathematical operations (Like (+) addition, (-) subtraction, (*) multiplication, (/) division) on the operands. We can categorize these arithmetic operations into two categories, including binary and unary operators. Here is the following table that helps to understand the working of these operators in C++:
| Operators | Name | Symbol | Description |
|---|---|---|---|
| Binary | Addition | + | It is utilized to add two values. |
| Subtraction | - | It is utilized to subtract one value from another value. | |
| Multiplication | * | It is utilized to multiply two values. | |
| Division | / | It is utilized to divide one value from another. | |
| Modulus | % | It is utilized to find the remainder value after the division. | |
| Unary | Increment | ++ | It is utilized to increase the value by 1. |
| Decrement | -- | It is utilized to decrease the value by 1. |
Arithmetic Operators Example:
Let us take a C++ program to calculate the Arithmetic operators.
Example
Output:
Addition: 19 Subtraction: 11 Multiplication: 60 Division: 3 Modulus: 3 Increment: 16 Decrement: 15
2. Relational Operators:
In C++, relational operators are mainly utilized to compare the values of two operands. It provides the result in Boolean values (0 and 1). If the comparison is true, it returns 1 value. On the other hand, if the comparison is false, it returns 0 value. There are several operators in relational operators. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description |
|---|---|---|
| Is Equal To operator | == | It checks both operands are equal or not. If the condition matches, it returns true; otherwise, false. |
| Greater Than | > | This operator is used to check the first operand value is greater than the second operand value. If the condition matches, it returns true; otherwise, false. |
| Greater Than or Equal To | >= | This operator is used to check the first operand value is greater than or equal to the second operand value. If the condition matches, it returns true; otherwise, false. |
| Less Than | < | This operator is used to check the first operand value is lesser than the second operand value. If the condition matches, it returns true; otherwise, false. |
| Less Than or Equal To | <= | It checks that the first operand value is lesser than or equal to the second operand value. If the condition matches, it returns true; otherwise, false. |
| Not Equal To | != | It checks both operand values are not equal. If the condition matches, it returns true; otherwise, false. |
Relational Operators Example:
Let us take a C++ program to calculate the relational operators.
Example
Output:
0 1 0 1 0 1
3. Logical Operators
In C++, logical operators are mainly utilized to perform logical operations on Boolean values. These operators are also utilized for combining two or multiple conditions. It provides the result in Boolean values (0 and 1). If the comparison is true, it returns 1 value. If the comparison is false, it returns 0 value. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description |
|---|---|---|
| Logical AND | && | If all operand's values are true or non-zero, it returns only the true value. |
| Logical OR | || | If either of the operand's values is true or non-zero, it returns true value. |
| Logical NOT | ! | If the operand value is false or zero, it returns a true value. |
Logical Operators Example:
Let us take a C++ program to illustrate the Logical Operators.
Example
Output:
The value of x && y is 1 The value of x || y is 1 The value of !y is 0
4. Bitwise Operators
Bitwise operators are mainly utilized to perform operations on the bit level. It works on individual bits. The operand is first converted into the bit level, and then the operation is performed on them. It performs bit by bit operation. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description |
|---|---|---|
| Binary AND | & | If this operator is available in both operands, it copies a bit to the calculated result. |
| Bitwise OR | | | If this operator is available in any of the operands, it copies a bit to the calculated result. |
| Bitwise XOR | ^ | If this operator is present in any of the operands but not both, it copies a bit to the calculated result. |
| Binary One's Complement | ~ | This operator changes the binary values 1 to 0 and 0 to 1. |
| Right Shift | >> | This operator shifts the value to the right by a number of bits specified by the right operands. |
| Left Shift | << | This operator shifts the value to the left by a number of bits specified by the right operands. |
Bitwise Operators Example:
Let us take a C++ program to illustrate the Bitwise Operators.
Example
Output:
x & y is 8 x | y is 14 x ^ y is 6 ~(x) is -15 x<<1 is 28 x>>1 is 7
5. Assignment Operators
These assignment operators are mainly utilized to assign the values to a variable. The most commonly utilized operator is the assignment operator (=). These operators enable us to change and update the value stored in the variable. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description |
|---|---|---|
| Assignment | = | It assigns the values from the right side operands to left side operand. |
| Add and Assignment | += | This operator first adds the right operand value to the left operand, and then the value is assign to the left operand. |
| Subtract and Assignment | -= | This operator first subtracts the right operand value from the left operand, and then the value is assign to the left operand. |
| Multiply and Assignment | *= | This operator first multiply the right operand with the left operand, and then the value is assign to the left operand. |
| Divide and Assignment | /= | This operator divides the left operand with the right operand, and then the value is assigns to the left operand. |
| Modulus and Assignment | %= | It takes remainders using two operands, and then the value is assign to the left operand. |
Assignment Operators Example:
Let us take a C++ program to illustrate the Assignment Operators.
Example
Output:
The value of x = 18 The value of x += y is 23 The value of x -= y is 18 The value of x *= y is 90 The value of x /= y is 18 The value of x %= y is 3
6. Ternary or Conditional Operator
The ternary or conditional operator performs operations on three operands. It is a conditional-based operator. It is mainly used to run a set of statements when the test expression is true, and another set of statements is executed when the test expression evaluates to false.
Syntax:
It has the following syntax:
Here, two conditions are occurred:
- If condition a is true, Expression1 is executed and gives the appropriate result.
- If condition a is false, Expression2 is executed and gives the appropriate result.
Ternary or Conditional Operator Example:
Let us take a C++ program to illustrate the Ternary and Conditional Operators.
Example
Output:
Enter the Player Score: 80 Player Won the Game.
7. Misc Operators
Misc operators is an abbreviation for miscellaneous operators. There are several misc operators that don't fit in any of the above categories, such as arithmetic, logical, bitwise, and many operators. Several miscellaneous operators in C++ are as follows:
- Sizeof() Operators:
The Sizeof() operator is utilized to return the size of the operand, variables, and data types in bytes.
Syntax:
It has the following syntax:
Example:
Let us take an example to illustrate the sizeof() operator in C++.
Example
Output:
4 1
- Comma Operator (,):
Comma operators are mainly utilized to separate the operands, variables, data types, etc. It is used to perform several operations. It evaluates the multiple expression and returns the last expression of the comma-separated list.
Syntax:
It has the following syntax:
Example:
Let us take an example to illustrate the comma operator in C++.
Example
Output:
34, 45
- Arrow and dot Operator:
Dot operators are mainly utilized to access members of an object. On the other hand, array operators are utilized for accessing the variables of the classes or structures via its pointer.
Syntax:
It has the following syntax:
Example:
Let us take an example to illustrate the arrow and dot operator in C++.
Example
Output:
10 10
- Casting Operator:
Using the casting operator, we can convert one data type to another data type.
Syntax:
Example:
Let us take an example to illustrate the casting operator in C++.
Example
Output:
The Value of (int)b is:4578 The Value of (int)b is:19
- Addressof Operator (&) and Pointer Operator (*)
It is mainly utilized to return the memory address of a variable. On the other hand, pointer operator is used to define a pointer to a variable.
Syntax:
Example:
Let us take an example to illustrate the addressof(&) operator and pointer (*) operator in C++.
Example
Output:
The address of the variable num is: 0x7ffe95d31498 The address of the variable var is: 30841
Operator Precedency and Associativity
Operator precedence and associativity are essential concepts in C++ programming. It defines the order, and part of the expression is calculated. Precedency shows that portion of the expression should be evaluated first. On the other hand, associativity shows which direction to calculate when precedency operators are in expression. Here, we will discuss both operators:
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator's direction to be evaluated, and it may be left to right or right to left.
Let us understand the precedence by the example given below:
The "data" variable will contain 148 because * (multiplicative operator) is evaluated before + (additive operator).
Operator Associativity:
In operator associativity, if the expression has multiple operators with similar precedence, the evaluation starts from left to right or right to left.
For example:
The precedence and associativity of C++ operators is given below:
| S.No | Category | Operator | Associativity |
|---|---|---|---|
| 1. | Scope Resolution | :: | Left to Right |
| 2. | Postfix | () [] -> . ++ - - | Left to right |
| 3. | Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
| 4. | Multiplicative | * / % | Left to right |
| 5. | Additive | + - | Right to left |
| 6. | Shift | << >> | Left to right |
| 7. | Relational | < <= > >= | Left to right |
| 8. | Equality | == !=/td> | Right to left |
| 9. | Bitwise AND | & | Left to right |
| 10. | Bitwise XOR | ^ | Left to right |
| 11. | Bitwise OR | | | Right to left |
| 12. | Logical AND | && | Left to right |
| 13. | Logical OR | || | Left to right |
| 14. | Conditional | ?: | Right to left |
| 15. | Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
| 16. | Comma | , | Left to right |
Next TopicC++ Identifiers