Operators perform any kind of operation like arithmetic, logical operation etc. Operators are divided into some categories according to its function.
Example:
sum=4+5 —> Here + and = are operators.
+ operator used for addition of 4 & 5 and result is assigned in sum by using = operator.
There are following categories on the basis of operators functionality –
— Arithmetic operators
— Relational Operators
— Logical operators
— Bit-wise operators
— Compound Assignment operators
— Pointer and Indirection operators
See the table for the various arithmetic operator:
Arithmetic Operator | Symbol | Example use | Remark |
---|---|---|---|
Addition | + | a+b | a and b are two variable |
Subtraction | - | a-b | a and b are two variable |
Multiplication | * | a*b | a and b are two variable |
Division | / | a/b | a and b are two variable |
modulus | % | a%b | a and b are two variable |
Assignment | = | a=b | a and b are two variable |
We are very familiar with almost all arithmetic operators use.I feel division and Modulus operators create confusion sometimes. So let us have a quick look at Division and Modulus operator.
Division operator gives quotient when one quantity is divided by another while Modulus gives remainder.
Example
case 1: if a> b; a and b are integer variable
1 2 3 4 5 |
a=10 ,b=8 then c=a/b=10/8=1 c=a%b=10%8=2 |
case 2: if a<b; a and b are integer variable
1 2 3 4 5 |
a=8 ,b=10 then c=a/b=8/10=0 c=a%b=8%10=8 |
case 3: if a=b; a and b are integer variable
1 2 3 4 5 |
a=10 ,b=10 then c=a/b=10/10=1 c=a%b=10%10=0 |
case 4: On Float variables modulus operation is invalid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int main( ) { double f_var1=4.5; double i_var2=91; double result_div; int result_mod; result_div =i_var2/f_var1; result_mod =i_var2%f_var1; printf("%lf\n",result_div); printf("%d",result_mod); return 0; } |
1 2 3 4 5 |
Output: error: invalid operands to binary % (have 'double' and 'double') result_mod =i_var2%f_var1; ^ |
The relational operators are used in the conditional statements to perform some operation.
See the table for relational operators list:
Comparision operators | Symbol | Example use |
---|---|---|
equal equal to | == | a==b |
not equal to | != | a!=b |
greater than | > | a>b |
less than | < | a |
greater than or equal to | >= | a>=b |
less than or equal to | <= | a<=b |
Logical operators work between the true(1) and false(0) condition. Logical operators use with the Relational Operator or conditional operator. There are mainly three logical operators –
— logical AND ( && )
— logical OR ( || )
— logical NOT ( ! )
Logical AND is used between the conditions when all conditions need to be true to perform some task.
Suppose there are two conditions Condition1 and Condition2, let us see logical AND(&&) operator use between these conditions.
Note: All Non-Zero number whether it is -ve or +ve, it is treated as True and only Zero consider as false.
Example
4&&5 =1(true)
4&&0 = 0(False)
-1&&1 = 1(true)
-4&&1=1(true)
(9>8)&&(4<5)=1(true)
Logical OR operator are used between the conditions when only one condition need to be true to perform some task.
Table for logical OR operator is –
Example:
4||5 =1(true)
4||0 = 1(true)
1||0 = 1(true)
-4||1=1(true)
(9>8)||(4>5)=1(true)
0||0 = False(0)
logical NOT works only single condition.It negate the current status of condition.
! (TRUE) = 0
! (False) = 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main( ) { unsigned int u32_var1=40; unsigned int u32_var2=50; unsigned char u8_logicalResult; /*logical AND operation*/ u8_logicalResult=u32_var1 && u32_var2; printf("Logical AND operation result=%u\n",u8_logicalResult); /*logical OR operation*/ u8_logicalResult=u32_var1 || u32_var2; printf("Logical OR operation result=%u\n",u8_logicalResult); /*logical NOT operation*/ u8_logicalResult=!u32_var1; printf("Logical NOT operation result=%u\n",u8_logicalResult); return 0; } |
1 2 3 4 5 |
Output: Logical AND operation result=1 Logical OR operation result=1 Logical NOT operation result=0 |
Logical operator considers whole digit as 1 or 0 and gives output in 1 and 0 i.e True and False while Bit-wise operator operates on every bit of a number.
Example
1 2 3 |
4(logical AND)5-----Output------>1(True) 4(Bitwise AND)5-------->0100(Bitwise AND)0101--------->0100 (AND operation on every bit) |
See the Table for Bitwise Operators –
Bit-wise Operator | Symbol | Example use |
---|---|---|
bit-wise AND | & | a&b |
bit-wise OR | | | a|b |
bit-wise XOR | ^ | a^b |
Bit-wise left Shift | << | a< |
bit-wise right Shift | >> | a>>b |
1 2 3 4 |
0 & 0 =0 0 & 1 =0 1 & 0 =0 1 & 1 =1 |
1 2 3 4 |
0 | 0 =0 0 | 1 =1 1 | 0 =1 1 | 1 =1 |
1 2 3 4 |
0 | 0 =0 0 | 1 =1 1 | 0 =1 1 | 1 =0 |
Example
E.g 1:
1 2 3 4 5 6 7 8 9 |
Bitwise AND: Perform 4&5 -- 4= 0 1 0 0 & & & & 5= 0 1 0 1 --------------- 0 1 0 0 ---------------- |
E.g 2:
1 2 3 4 5 6 7 8 9 |
BitWise OR: Perform 4 | 5 -- 4= 0 1 0 0 | | | | 5= 0 1 0 1 --------------- 0 1 0 1 ---------------- |
E.g 3:
1 2 3 4 5 6 7 |
Bitwise left shift: Perform 10<<2 ---- 10 = 0 0 0 0 1 0 1 0 10<<1 = 0 0 0 1 0 1 0 0 (<<1)--step1 10<<2 = 0 0 1 0 1 0 0 0 (<<2)--step2 |
E.g 4:
1 2 3 4 5 6 7 |
Bitwise Right shift: Perform 10>>2 ---- 10 = 0 0 0 0 1 0 1 0 10>>1 = 0 0 0 0 0 1 0 1 (>>1)--step1 10>>2 = 0 0 0 0 0 0 1 0 (>>2)--step2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<stdio.h> int main( ) { unsigned int u32_var1=40; unsigned int u32_var2=50; unsigned int u32_bitResult; /*Bitwise AND operation*/ u32_bitResult=u32_var1 & u32_var2; printf("Bitwise AND operation result=%u\n",u32_bitResult); /*Bitwise OR operation*/ u32_bitResult=u32_var1 | u32_var2; printf("Bitwise OR operation result=%u\n",u32_bitResult); /*Bitwise XoR operation*/ u32_bitResult=u32_var1 ^ u32_var2; printf("Bitwise XoR operation result=%u\n",u32_bitResult); /*Bitwise Left Shift operation*/ u32_bitResult=u32_var1<<3; printf("Bitwise Left Shift operation result=%u\n",u32_bitResult); /*Bitwise Right Shift operation*/ u32_bitResult=u32_var1>>3; printf("Bitwise Right Shift operation result=%u\n",u32_bitResult); return 0; } |
1 2 3 4 5 6 7 |
Output: Bitwise AND operation result=32 Bitwise OR operation result=58 Bitwise XoR operation result=26 Bitwise Left Shift operation result=320 Bitwise Right Shift operation result=5 |
Compound assignment operators are the combination of two operations. the first operation may be any arithmetic or bitwise operation but second must be assignment operation.
So instead of writing two separate statement for two operations, we can finish it by using single Compound assignment operators.
For Example, Perform addition of a and b and assign that sum to a.
we have two methods for this:
a=a+b;
a+=b; (+= compound addition assignment operator)
Compound assignment operators. | Symbol | Example Use | Equivalent Operation |
---|---|---|---|
Addition assignment | += | a+=b | a=a+b |
Subtraction assignment | -= | a-=b | a=a-b |
Multiplication assignment | *= | a*=b | a=a*b |
Division assignment | /= | a/=b | a=a/b |
modulo assignment | %= | a%=b | a=a%b |
bit-wise AND assignment | &= | a&=b | a=a&b |
bit-wise OR assignment | |= | a|=b | a=a|b |
bit-wise XOR assignment | ^= | a^=b | a=a^b |
bit-wise Left Shift assignment | <<= | a<<=b | a=a< |
bit-wise Right shift assignment | >>= | a>>=b | a=a>>b |
The ternary operator is the combination of three operators- relational,?
Syntax:
1 |
(condition)? statment1 : statment2; |
If the condition is true then statment1 will execute otherwise statment2 if the condition is false.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int main() { int a, b; printf("Enter a :"); scanf("%d",&a); printf("\nEnter b :"); scanf("%d",&b); (a>b) ? printf("\n a is greater") : printf("\n b is greater"); return 0; } |
1 2 3 4 5 |
Output: Enter a :45 Enter b :90 b is greater |