Operators in C
M4-R5.1 · Chapter 4: Building IoT applications · 3 min read
Operators in C
C operators are symbols that are used to perform Sone specific operations on operands.
There are following types of operators in C.
- Arithmetic Operators
- Comparison (Relational) Operator
- Logical Operators
- Assignment Operators
- Bitwise Operators
1.Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on the operands. The following operators are known as C arithmetic operators.
|
Operator |
Description |
Example |
|
+ |
Addition |
10+20 = 30 |
|
- |
Subtraction |
20-10 = 10 |
|
* |
Multiplication |
10*20 = 200 |
|
/ |
Division |
20/10 = 2 |
|
% |
Modulus (Remainder) |
20%10 = 0 |
|
++ |
Increment |
Int a=10; a++; Now a = 11 |
|
-- |
Decrement |
Int a=10; a--; Now a = 9 |
2.Comparison Operators
The C comparison operator compares the two operands. The comparison operators are as follows:
|
Operator |
Description |
Example |
|
== |
Is equal to |
10==20 = false |
|
!= |
Not equal to |
10!=20 = true |
|
> |
Greater than |
20>10 = true |
|
>= |
Greater than or equal to |
20>=10 = true |
|
< |
Less than |
20<10 = false |
|
<= |
Less than or equal to |
20<=10 = false |
3.Logical Operators
The following operators are known as C logical operators.
|
Operator |
Description |
Example |
|
&& |
Logical AND ( It returns true when both expression are true) |
(10==20 && 20==33) = false |
|
|| |
Logical OR( It returns true when one of both equations is true) |
(10==20 || 20==20) = true |
|
! |
Logical Not (It return true when expression give false) |
!(10==20) = true |
4.Assignment Operators
The following operators are known as C assignment operators.
|
Operator |
Description |
Example |
|
= |
Assign |
var a=6; |
|
+= |
Add and assign |
int a=10; a+=20; Now a = 30 |
|
-= |
Subtract and assign |
int a=20; a-=10; Now a = 10 |
|
*= |
Multiply and assign |
int a=10; a*=20; Now a = 200 |
|
/= |
Divide and assign |
int a=10; a/=2; Now a = 5 |
|
%= |
Modulus and assign |
int a=10; a%=2; Now a = 0 |
5.Bitwise Operators
The bitwise operators perform bitwise operations on operands. The bitwise operators are as follows:
|
Operator |
Description |
Example |
|
& |
Bitwise AND |
5 & 1 = 1 5 & 4 = 4 |
|
| |
Bitwise OR |
5 | 1 = 5 5 | 4 = 5 |
|
^ |
Bitwise XOR |
5 ^ 1 = 4 5 ^ 4 = 1 |
|
~ |
Bitwise NOT |
(~10) = -11 |
|
<< |
Bitwise Left Shift |
(10<<2) = 40 |
|
>> |
Bitwise Right Shift |
(10>>2) = 2 |
Note –
- Formula for left shift – number * 2n
- Formula for Right shift – number / 2n