C Operators
Operator is special type of symbol that are perform relational,comparison,logical and mathematical type operations.

In c programming operator are classified into following category.

- Arithmetic Operator
- Logical Operators
- Relational Operators
- Assignment Operators
- Bitwise Operators
Arithmetic Operator
Arithmetic operators are used to perform mathematical operation of two operands. using of these operator we can easily to calculate like addition, subtraction,multiplication, division. and also can solve mathematical equation.
Operator | Name |
---|---|
+ | Addition( plus sign) |
- | Subtract (minus sign) |
* | multiplication (* symbol) |
/ | Divide |
% | remainder operator |
Example
/*
Example of Arithmetic Operator
*/
/*Header file*/
#include <stdio.h>
int main(){
int a=10,b=5,resutl;
//add
resutl=a+b;
printf("Addition [%d + %d] : %d\n",a,b,resutl);
//subtract
resutl=a-b;
printf("subtraction [%d - %d] : %d\n",a,b,resutl);
//multiply
resutl=a*b;
printf("multiply [%d * %d] : %d\n",a,b,resutl);
//divide
resutl=a/b;
printf("division [%d / %d] : %d\n",a,b,resutl);
//modules
resutl=a%b;
printf("remainder [%d %% %d] : %d\n",a,b,resutl);
return 0;
}
Output
Addition [10 + 5] : 15
subtraction [10 - 5] : 5
multiply [10 * 5] : 50
division [10 / 5] : 2
remainder [10 % 5] : 0
Logical Operators
logical operator are used to compare simply based on multiple condition. this condition are based on operand. operand and logical operator are combine together and make a decision expression. this expression are returns boolean value. this boolean value are (1,0) True and False . if expression is valid then its True otherwise False. Here given of three logical operators.
Operator | Name |
---|---|
&& | Logical AND operator. |
|| | Logical OR Operator |
! | Logical NOT Operator |
For example.
//Example to use Logical Operators
#include<stdio.h>
int main(){
//case 1 check logical not
if(!(4>=6)){
printf("4 is not greater than or equal to 6");
}else{
printf("4 is less than to 6");
}
//case 2 check logical and
if(4>2 && 3>5){
//when given both expression result is true
printf("\nLogical And both condition is true");
}else{
printf("\nLogical And both condition is not False");
}
//Case 3 check logical and
if(4>2 || 3>5){
printf("\nLogical And Any one condition is true");
}else{
printf("\nLogical or both condition is False");
}
return 0;
}
Output
4 is not greater than or equal to 6
Logical And both condition is not False
Logical And Any one condition is true
Relational Operators
relational operator are used to compare two values. this is work on two operand or two values so it also called binary operator.result of relational operator is boolean value( True and false). here given six relational operator.
Operator | Name |
---|---|
> | greater than |
< | less than |
== | equal to equal to |
>= | greater than equal to |
<= | less than equal to |
!= | Not equal to |
For example.
//Example of Relational Operators
#include<stdio.h>
int main(){
int a=6,b=4;
//View relational operators result
printf("a %d b %d\n",a,b);
printf("a>b %d\n",a>b ); //true 1
printf("a<b %d\n",a<b ); //false 0
printf("a>=b %d\n",a>=b );
printf("a<=b %d\n",a<=b );
printf("4==4 %d\n",4==4);
printf("4!=4 %d\n",4!=4);
printf("4!=5 %d\n",4!=5);
return 0;
}
Output
a 6 b 4
a>b 1
a<b 0
a>=b 1
a<=b 0
4==4 1
4!=4 0
4!=5 1
Assignment Operators
assignment operators are used to initialize and assign new value to variables. there are denoted as (=) equal sign.
Operator | Meaning |
---|---|
= | equal |
*= | Multiply and assign |
+= | Add and assign |
/= | Divide and assign |
-= | Subtract and assign |
%= | calculate remainder and assign |
For example
//Example of Assignment Operators
#include<stdio.h>
int main(){
int data=5;//assign value using = operator
printf("%d\n",data);
data+=4;//same as data=data+4
printf("%d\n",data);
data-=4;//same as data=data-4
printf("%d\n",data);
data*=4;//same as data=data*4
printf("%d\n",data);
data/=4;//same as data=data/4
printf("%d\n",data);
data%=4;//same as data=data%4
printf("%d\n",data);
return 0;
}
Output
5
9
5
20
5
1
Bitwise Operators
Bitwise Operator are work in bit level. this bits are represented in from of binary numbers (0,1).perform all operation in (CPU) is work on bit-level. here given six Bitwise operators.
Operator | Meaning |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
>> | Shift Right |
<< | Shift left |
For example of Bitwise & Operators
//Example of Bitwise & Operators
#include<stdio.h>
int main(){
//example of Bitwise &
/**---
5 = 0 1 0 1
4 = 0 1 0 0
------------
& = 0 1 0 0 =4
--**/
printf("5&4 = %d\n",5&4);
/**---
5 = 0 1 0 1
10 = 1 0 1 0
------------
& = 0 0 0 0 =0
--**/
printf("5&10 = %d\n",5&10);
//example of Bitwise |
/**---
5 = 0 1 0 1
4 = 0 1 0 0
------------
| = 0 1 0 1 =5
--**/
return 0;
}
Output
5&4 = 4
5&10 = 0
For example of Bitwise | Operators
//Example of Bitwise | (or) Operators
#include<stdio.h>
int main(){
//Explanation of Bitwise |
/**---
5 = 0 1 0 1
4 = 0 1 0 0
------------
| = 0 1 0 1 =5
--**/
printf("5|4 = %d\n",5|4);
/**---
5 = 0 1 0 1
10 = 1 0 1 0
------------
| = 1 1 1 1 =15
--**/
printf("5|10 = %d\n",5|10);
return 0;
}
Output
5|4 = 5
5|10 = 15
Example of Bitwise ^ Operators
//Example of Bitwise ^ Operators
#include<stdio.h>
int main(){
//Explanation of Bitwise ^
/**---
5 = 0 1 0 1
4 = 0 1 0 0
------------
^ = 0 0 0 1 =1
--**/
printf("5^4 = %d\n",5^4);
/**---
13 = 1 1 0 1
10 = 1 0 1 0
------------
^ = 0 1 1 1 =7
--**/
printf("13^10 = %d\n",13^10);
return 0;
}
Output
5^4 = 1
13^10 = 7
Example of Bitwise ~ (not) Operators
//Example of Bitwise ~ Operators
#include<stdio.h>
int main(){
//Example of Bitwise ~
/**---
5 = 0 1 0 1
add 1 1
------------
~ 0 1 1 0 = 6
mutiply - to result
=-6
--**/
printf("~5 = %d\n",~5);
/**---
13 = 1 1 0 1
1
------------
^ = 1 1 1 0 =14
mutiple -1 to result
=-14
--**/
printf("13 = %d\n",~13);
/**---
13 = 0 0 1 1 0 1
-13 = 1 1 0 0 1 0 (simple complement of 13)
1
------------
1 1 0 0 1 1 (add 1)
------------
-13 0 0 1 1 0 0 (inverse) =12
--**/
printf("~-13 = %d\n",~-13);
return 0;
}
Output
~5 = -6
13 = -14
~-13 = 12
Example of Bitwise >> Operators
//Example of Bitwise >> Operators
#include<stdio.h>
int main(){
/**--
14 : 1 1 1 0
----------------
(14>>2)-> -> 1 1 1 0
----------------
1 1 =3
--**/
printf("%d\n",14>>2 );
/**--
32 : 1 0 0 0 0 0
----------------
(32>>4)-> -> -> -> 1 0 0 0 0 0
---------------------
1 0 =2
--**/
printf("%d\n",32>>4 );
return 0;
}
Output
3
2
Example of Bitwise << Operators
//Example of Bitwise << Operators
#include<stdio.h>
int main(){
/**--
14 : 0 0 1 1 1 0
----------------
<- <-
14<<2 : 1 1 1 0 0 0
----------------
1 1 1 0 0 0 = 56
--**/
printf("%d\n",14<<2 );
/**--
32 : 0 0 0 0 1 0 0 0 0 0
---------------------------------
<- <- <- <-
(32<<4)1 0 0 0 0 0 0 0 0 0 =512
--**/
printf("%d\n",32<<4 );
return 0;
}
Output
56
512
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment