Types of operators in C
Arithmetic Operators
Arithmetic operators: +, -, *, /, %. These are binary operators as they operate on 2 operands at a time. Note that each of these operators can work with int, float or char.
Basic arithmetic operators provided by C++ are summarized in table below.
Operator | Name | Example |
+ | Addition | 12 + 4 // gives 16 |
– | Subtraction | 3.98 – 2 // gives -1.98 |
* | Multiplication | 5 * 3.2 // gives 16.0 |
/ | Division | 11 / 2 // gives 5.5 |
% | Remainder | 10 % 4 //gives 2 |
Operators +, -, *, / are self-explanatory.
Modulo division operator (%):
This operator returns remainder after integer division. It is important to note that during modulo division, the sign of the result is always the sign of first operand (i.e., numerator).So, if you are interested to get remainder after division operation, we shoulduse %.
Operation | Result | Remark |
6 % 3 | 0 | After dividing 6 with 3, we get remainder 0 |
3 % 3 | 0 | After dividing 3 with 3, we get remainder 0 |
9 % 3 | 0 | After dividing 9 with 3, we get remainder 0 |
5%3 | 2 | After dividing 5 with 3, we get remainder 2 |
14 % 3 | 2 | After dividing 14 with 3, we get remainder 2 |
-14% -3 | -2 | Numerator is -14. So, we get negative sign in result |
3%5 | 3 | We cannot divide 3 with 5. So, remainder is numerator itself. |
-22% -6 | -4 | Numerator is -14. So, we get negative sign in result |
10 % 3 | 1 | After dividing 10 with 3, we get remainder 1 |
Points to remember
- Used with division function
- Modulo operator work with only int/char
- Use of % with float/double not allowed
- Sign of output is always same as the sign of numerator irrespective of the sign of the denominator
Logical Operators
Logical operators are used to combine two or more conditions in the program. There are3 logical operators namely: NOT, AND, OR. Note that these operators yield BOOLEAN result. BOOLEAN means either true or False.
S. No. | Operator | Meaning | Example |
1 | ! | Logical Negation | !(5 ==5) //returns FALSE |
2 | && | Logical AND | 5 < 6 && 6 < 6 // returns FALSE |
3 | || | Logical OR | 5 < 6 || 6 < 5 // returns TRUE |
Q. The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 – First division
Percentage between 50 and 59 – Second division
Percentage between 40 and 49 – Third division
Percentage less than 40 – Fail
Writea program to calculate the division obtained by the student.
Relational Operators
Relational operators are used to compare two quantities for big/small/equal etc. note that these quantities can be variables/constants/expressions. These operators compare two operands, so these are binary operators. Characters can also be compared as they are represented internally as ASCII codes (integers).
Relational operator returns either TRUE or FALSE. In C++, non-zero (+ve or –ve) value represents TRUE and zero represents FALSE. There are 6 relational operators, which are described in below table.
Operator | Meaning | Example |
= = | Equality | 5 == 5 // returns True |
!= | Not Equal to | 5 != 5 // returns False |
< | Less Than | 5 < 5.5 // returns True |
<= | Less Than or Equal | 5 <= 5 // returns True |
> | Greater Than | 5 > 5.5 // returns True |
>= | Greater Than or Equal | 6.3 >= 5 // returns True |
For example, the statement x = = y; tests whether left-hand side (LHS) value and right-hand side (RHS) value are equal or not.
a == b; tests whether the value of a is equal to b
a = b; simply assigns b to a
Increment/decrement Operators
The increment (++) and decrement (–) operators provide a convenient way of, respectively, adding and subtracting 1 from a numeric variable. These are summarized in the following table. The examples assume the following variable definition:
int a = 5;
int b = 6
Operator | Meaning | Example |
++ | Increment | a++ // gives 6 |
— | Decrement | b– // gives 5 |
Prefix & postfix operators have same effect if they are used in an isolated C++ statements. a++ and ++a are the same, if they are used in isolated statements.
Pre-increment: ++ x; | Increments the value of variable x by 1 |
Post-increment: x++; | Increments the value of variable x by 1 |
Pre-decrement: –x; | Decrements the value of variable x by 1 |
Post-decrement: x–; | Decrements the value of variable x by 1 |
In other words, the statements ++x or x++; is equivalent of the following statement: x = x + 1;
And –y or y–; is equivalent of the following statement: y = y -1.
Note: The increment and decrement operators can be used only with variables, not with constants.
Hence a++, x++ are legal. 6++, 10++ are invalid.
EXPRESSION | OUTPUTS |
x=5 y=x++ | x=6 y=5 |
x=5 y=++x | x=6 y=6 |
a=8 b=–a | a=7 b=7 |
a=8 b=a– | a=7 b=8 |
x=5 , y=8 z=x+y– | x=5 y=7 z=13 |
x=5 y=8 z=x+–y | x=5 y=7 z=12 |
Prefix and postfix operators have different effects when used in association with some other operators in a C statement.
C++ offers increment (++) and decrement (- -) operators. ++ operator increments the value of the variable by 1. — decrements the value of the variable by 1. These two are unary operators as they operate on only one operand.
Bitwise operators
These operators can operate upon ints and chars but not on floats and doubles.
S. No. | Symbol | Meaning |
1 | ~ | 1’s complement |
2 | << | Left shift |
3 | >> | Right shift |
4 | & | Bitwise AND |
5 | | | Bitwise OR |
7 | ^ | Exclusive OR |
Bitwise AND: c=a & b: If the corresponding bits are both 1 then the resultant bit is 1 , otherwise 0.
Bitwise OR: c=a | b: If the corresponding bits are both 0 then the resultant bit is 0 , otherwise 1.
Exclusive OR: c=a ^ b: If the corresponding bits are both same then the resultant bit is 0 , otherwise 1.
1’s Compliment: c=~a: 0 bit is converted to 1 and vice versa
Left Shift: c=a<<b: In the left shift operator(<<), b number of 0’s are added on the right side.Right Shift: c=a>>b: In the right shift operator(>>), the right most bit is removed and b number of 0’s are added on the left side for right shift by 1.
Some other special operators
Comma Operator
The comma operator is used between multiple expressions inside a set of parentheses. These expressions are then evaluated from left to right and the entire expression assumes the value of the last one evaluated.
For e.g. we can form an expression by separating two sub expressions with a comma. The result is as follows:
- Both expressions are evaluated, with the left expression being evaluated first.
- The entire expression evaluates to the value of the right most expression.