Operators are the underpinning of any programming language. We can characterize operators as symbols that assist us with performing specific mathematical and logical calculations on operands. As such, we can say that an operator operates the operands. For instance, ‘+’ is an operator utilized for addition, as displayed beneath:  


c = a + b;


Here, ‘+’ is the operator known as the expansion administrator and ‘a’ and ‘b’ are operands. The addition operator advises the compiler to add both of the operands ‘a’ and ‘b’. 

C++ operator


The usefulness of the C/C++ programming language is fragmented without the utilization of operators.


C/C++ has many built-in operators and can be arranged into 6 types:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Other Operators


The above operators have been examined in detail: 


1. Arithmetic Operators: 

These operators are utilized to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operators are of two kinds: 

a) Unary Operators: Operators that operate or work with a solitary operand are unary operators. For instance: Increment(++) and Decrement(–) Operators

int val = 5; ++val; // 6

b) Binary Operators: Operators that operate or work with two operands are paired operators. For instance: Addition(+), Subtraction(-), multiplication(*), Division(/) operators

int a = 7; int b = 2; cout<<a+b; // 9


2. Relational Operators:

These are utilized for the examination of the values of two operands. For instance, checking assuming one operand is equivalent to the next operand or not, an operand is greater than the other operand or not, and so forth a. Portion of the relational operators are (==, >= , <= )(See this article for more reference).

int a = 3; int b = 5; a < b; // operator to check if a is smaller than b


3. Logical Operators:

Logical Operators are utilized to combining two or more conditions/constraints or to supplement the assessment of the original condition in thought. The result of the activity of a logical operator is a Boolean value either true or false. 

For example, the logical AND represented as ‘&&’ operator in C or C++ returns true when both the conditions viable are fulfilled. Otherwise, it returns false. In this manner, a && b returns true when both a and b are true (i.e. non-zero)

(4 != 5) && (4 < 5); // true


4. Bitwise Operators: 

The Bitwise operators are utilized to perform bit-level operations on the operands. The operators are first changed over to bit-level and then the estimation is performed on the operands. The mathematical activities like addition, subtraction, multiplication, etc. can be performed at digit level for faster processing. For instance, the bitwise AND represented as & operator in C or C++ takes two numbers as operands and does AND on all  of two numbers. The result of AND is 1 only if both bits are 1.

int a = 5, b = 9; // a = 5(00000101), b = 9(00001001) cout << (a ^ b); // 00001100 cout <<(~a); // 11111010


5. Assignment Operators: 

Assignment operators are accustomed to relegating worth to a variable. The left side operand of the assignment administrator is a variable and the right side operand of the assignment administrator is a worth. The value on the right side should be of the similar data type as the variable on the left side if not the compiler will raise an error. 

Various kinds of assignment operators are displayed below: 
a. “=”: This is the least complex assignment administrator. This operator is utilized to relegate the worth on the right to the variable on the left. 
For example: 

a = 10; b = 20; ch = 'y';

b. “+=”: This operator is blend of ‘+’ and ‘=’ administrators. This operator first adds the current worth of the variable on left to the worth on the right and then assigns the outcome to the variable on the left. 
For example:

(a += b) can be composed as (a = a + b) If initially value put away in a is 5. Then (a += 6) = 11.

c. “-=”: This operator is a blend of ‘-‘ and ‘=’ operators. This administrator first deducts the worth on the right from the current value of the variable on left and then assigns the result to the variable on the left. 
For instance: 

(a -= b) can be composed as (a = a - b) If initially value  put away in a is 8. Then (a -= 6) = 2.

d. “*=”: This operator is a blend of ‘*’ and ‘=’ operators. This administrator first duplicates the current value of the variable on left to the value on the right and afterward allots the result to the variable on the left. 
For example: 

(a *= b) can be composed as (a = a * b) If initially, the value put away in a is 5. Then (a *= 6) = 30.

e. “/=”: This operator is a blend of ‘/’ and ‘=’ operators. This operator first partitions the current worth of the variable on left by the value on the right and afterward relegates the result to the variable on the left. 
For example:

(a /= b) can be composed as (a = a / b) If initially, the value put away in a is 6. Then (a /= 2) = 3.


6. Other Operators: 

Aside from the above administrators, there are few different operators accessible in C or C++ used to play out some specific tasks. Some of them are talked about here: 

a. sizeof operator: 

sizeof is quite utilized in the C/C++ programming language.

It is a compile-time unary operator which can be utilized to figure the size of its operand.

The consequence of sizeof is of the unsigned fundamental type which is generally indicated by size_t.

Essentially, the sizeof the operator is utilized to figure the size of the variable.


b. Comma Operator: 

The comma operator (addressed by the token) is a parallel operator that assesses its first operand and disposes of the result, it then evaluates the subsequent operand and returns this worth (and type).

The comma operator has the least priority of any C operator.

Comma goes about as both operator and separator. 


c. Conditional Operator: 

The conditional operator is of the form Expression1? Expression2: Expression3.

Here, Expression1 is the condition to be assessed. If the condition(Expression1) is True then we will execute and return the aftereffect of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the consequence of Expression3.

We might supplant the utilization of if..else statements with conditional administrators. 


Operator Precedence Chart

The beneath table describes the priority order and associativity of administrators in C / C++. The precedence of the operator diminishes from top to bottom. 


Precedence Operator Description Associativity
1 () Parentheses (function call) left-to-right
[] Brackets (array subscript)  
. Member selection via object name  
-> Member selection via a pointer  
++/– Postfix increment/decrement  
2 ++/– Prefix increment/decrement right-to-left
+/- Unary plus/minus  
!~ Logical negation/bitwise complement  
(type) Cast (convert value to temporary value of type)  
* Dereference  
& Address (of operand)  
sizeof Determine size in bytes on this implementation  
3 *,/,% Multiplication/division/modulus left-to-right
4 +/- Addition/subtraction left-to-right
5 << , >> Bitwise shift left, Bitwise shift right left-to-right
6 < , <= Relational less than/less than or equal to left-to-right
> , >= Relational greater than/greater than or equal to left-to-right
7 == , != Relational is equal to/is not equal to left-to-right
8 & Bitwise AND left-to-right
9 ^ Bitwise exclusive OR left-to-right
10 | Bitwise inclusive OR left-to-right
11 && Logical AND left-to-right
12 || Logical OR left-to-right
13 ?: Ternary conditional right-to-left
14 = Assignment right-to-left
+= , -= Addition/subtraction assignment  
*= , /= Multiplication/division assignment  
%= , &= Modulus/bitwise AND assignment  
^= , |= Bitwise exclusive/inclusive OR assignment  
<>= Bitwise shift left/right assignment  
15 , expression separator left-to-right

Previous                                                                                         Next