These are the tokens which are used to do some specific task, condition testing, calculation c is rich in operator.
The value on which the operator operates is called an operand.
Type of Operators
1. Arithmetic Operator
2. Increment/Decrement Operator
3. Relational Operator
4. Conditional /Ternary Operator
5. Logical/Short circuit Operator
6. Bitwise Operator
7. Sizeof Operator
8. Comma Operator
9. Assignment and Shorthand Operator
1. Arithmetic Operator
It is used to do mathematic calculation like: + , - , * , / , % etc.
Example if a=4 and b=2
a+b = 6
a-b = 2
a*b = 8
a/b = 2
a%b = 0
Example of Assignment operator
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j=2,k;
clrscr();
k=i+j;
printf("%d+%d=%d\n",i,j,k);
k=i-j;
printf("%d-%d=%d\n",i,j,k);
k=i*j;
printf("%d*%d=%d\n",i,j,k);
k=i/j;
printf("%d/%d=%d\n",i,j,k);
float k=i/j;
printf("%d/%d=%f\n",i,j,k);
k=i%j;
printf("%d//%d=%d\n",i,j,k);
getch();
}
Output
5+2=7
5-2=3
5*2=10
5/2=2
5/2=2.0
5%2=1
2. Increment/Decrement Operator
Increment operator [++] adds one to each value Decrement operator [- -] subtract one to each value. It is of two types
1. Prefix ++ , --
2. Postfix ++ , --
Prefix -> It first add the value and then operates
Postfix -> It first operates and then add value
Example of Increment/Decrement
#include<stdio.h>
#include<conio.h>
void main(){
int i=2;
clrscr();
i=i++ + ++i + ++i + i++;
printf("%d\n%d\n%d",i++,++i,++i);
getch();
}
Output
20
20
19
3. Relational Operator
It is also known as comparison operator it determine the relation between the value its answer come in true or false. (< > , <= , >= , == , !=)
Example - if a=10 and b=7
a>b = false
a>=b = false
a<b = true
a<=b = true
a==b = false
a!=b = true
Example of Relational Operator
#include<stdio.h>
#include<conio.h>
void main(){
int i=2,j=3;
clrscr();
i=i++<++j<++i;
printf("%d%d%d",i++,++j,++i);
getch();
}
Output
3
5
3
4. Conditional/Ternary Operator
It stores value depends upon a condition It is also known as ternary operator syntax:-
Condition? True: False
Example of Ternary/Conditional Operator
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j=7,k=2,min;
clrscr();
min=i<j?(i<k?i:k):(j<k?j:k);
printf("min=%d",min);
getch();
}
Output
Min=2
5. Logical/Short circuit Operator
It is used to combine the existing expression .It is also used to examine the arithmetic and relational operator. It is of 3 type
1. Logical AND [&&]
When both the condition are true then it works
Experation 1 && Experation 2
False -> no
Post increment/decrement in exp1 and store 0(zero) left side variable.
Experation 1 && Experation 2
True -> yes
Post increment/decrement in exp1 and check exp2
If exp2 false
Post increment/decrement in exp2
Store 0(zero) into left side variable
If exp2 true
Post increment/decrement in exp2
Store 1(one) into left side variable
A | B | X=A.B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example of Logical AND [&&]
#include<stdio.h>
#include<conio.h>
void main(){
int i=3,j=4,k=2;
clrscr();
k=i++<++j&&k++<++i;
printf("%d%d%d",i,j,k);
getch();
}
Output
5 5 1
2. Logical OR [||]
When any one condition is true then it operates
A | B | X=A+B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example of Logical OR [||]
#include<stdio.h>
#include<conio.h>
void main(){
int i=3,j=4,k;
clrscr();
k=i++<++j||++i<++j;
printf("%d%d%d",i,j,k);
getch();
}
Output
4 5 1
3. Logical NOT [!]
It reverses the value of the operands
A | X=A |
1 | 0 |
0 | 1 |
Example of Logical NOT [!]
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j;
clrscr();
j=!i;
printf("%d%d",i,j);
getch();
}
Output
5 0
6. Bitwise Operator
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables
for & , | , ^ , >> , << , ~ are as follows:
1. Bitwise AND [&]
Binary AND Operator copies a bit to the result
if it exists in both operands.
Example - (A & B) will give 12 which is 0000 1100
P | Q | P & Q |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example of Bitwise AND [&]
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j=7,k;
clrscr();
k=i&j;
printf("%d",k);
getch();
}
Output
5
2. Bitwise OR [|]
Binary OR Operator copies a bit if it exists in either operand.
Example - (A | B) will give 61 which is 0011 1101
P | Q | P | Q |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example of Bitwise OR [|]
#include<stdio.h>
#include<conio.h>
void main(){
int i=7,j=11,k=2;
clrscr();
k=i++|++j|++i|k++;
printf("%d\n%d\n%d",i,j,k);
getch();
}
Output
9 12 15
3. Bitwise XOR [^]
Binary XOR Operator copies the bit if it is set in one operand but not both.
Example - (A ^ B) will give 49 which is 0011 0001
P | Q | P | Q |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example of Bitwise XOR [^]
#include<stdio.h>
#include<conio.h>
void main(){
int i=27,j=-93,k;
clrscr();
k=printf("%d\n",i)^printf("%d+%d",j++,++j);
printf("%d",k);
getch();
}
Output
-27
-92+-92 3
4. Bitwise Right Sift [>>]
Binary Right Shift Operator. The left operands value is moved right by the number of bits
specified by the right operand.
Example - A >> 2 will give 15 which is 0000 1111
Example of Bitwise Right Sift [>>]
#include<stdio.h>
#include<conio.h>
void main(){
int i=23,j;
clrscr();
j=i>>2;
printf("%d\n%d",i,j);
getch();
}
Output
23
5
5. Bitwise Lift Sift [<<]
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
Example - A << 2 will give 240 which is 1111 0000
Example of Bitwise Lift Sift [<<]
#include<stdio.h>
#include<conio.h>
void main(){
int i=17,j;
clrscr();
j=i<<3;
printf("%d\n%d",i,j);
getch();
}
Output
17
136
6. Ones Complement [~]
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
Example - (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
Example of Ones Complement [~]
#include<stdio.h>
#include<conio.h>
void main(){
int i=1,j;
clrscr();
j=~i;
printf("%d",j);
getch();
}
Output
-2
7. Sizeof Operator
Sizeof operator returns the size of a variable.
For example, sizeof(a), where ‘a’ is integer, and will return 4.
Sizeof (Data_Type)
Sizeof (Variable)
Sizeof ( Value)
Example of Sizeof Operator
#include<stdio.h>
#include<conio.h>
void main(){
int i=5;
clrscr();
printf("%d\n",sizeof(i));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(10));
printf("%d\n",sizeof(3.45));
getch();
}
Output
2 2 2 8
8. Comma Operator [,]
Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.
Example - (x=18,y=2,=20)
Example of Comma Operator [,]
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j=7,k=6,l;
clrscr();
l=i,j,k;
printf("%d%d%d%d",i,j,k,l);
getch();
}
Output
5 7 6 5
9. Assignment and Shorthand Operator
This operator is used to store a value in variable of any variable. In left part is always used to variable.
Operator | Description | Example |
= | Simple assignment operator, Assigns values from right side operands to left side operand. | C = A + B will assign value of A + B into C |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand. | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand. | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand. | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator.
| C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator.
| C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator.
| C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator.
| C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator.
| C |= 2 is same as C = C | 2 |
Example of Assignment and Shorthand Operator
#include<stdio.h>
#include<conio.h>
void main(){
int i=5;
clrscr();
i+=5;
printf("%d",i);
getch();
}
Output
10
0 Comments