Operator in C Language


👉Operator

It is a special symbol which is used to perform logical or mathematical operation on data or variable.

👉Operand

It is a data or variable on which the operation is to be performed.

👉Types of Operator

⇒Arithmetic Operators
⇒Relational Operators
⇒Logical Operators
⇒Assignment Operators
⇒Bitwise Operators
⇒Increment/Decrement Operators
⇒Conditional Operators
⇒Special Operator

👉Arithmetic Operators

SymbolOperationExample
+Additionx+y
-Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulusx%y
#include<stdio.h>
int main()
{
int x=16,y=3;//For operation
int add,sub,multi,div,mod;//To store the result
add=x+y;
sub=x-y;
multi=x*y
div=x/y;
mod=x%y;
printf("Addition=%d\n",add);
printf("Subtraction=%d\n",sub);
printf("Multiplication=%d\n",multi);
printf("Division=%d\n",div);
printf("Modulus=%d\n",mod);
}

### Output ###
Addition=19
Subtraction=13
Multiplication=48
Division=5
Modulus=1 //because % stores remainder 

👉Relational Operators

SymbolOperationExample
==Equal to2==3 returns 0
!=Not equal to2!=3 returns 1
>Greater than2>3 returns 0
<Less than2<3 returns 1
>=Greater than or equal to2>=3 returns 0
<=Less than or equal to2<=3 returns 1


👉Logical Operators


 

(x>y)&&(x>z)

Here this expression returns true if both conditions are true.

(x>y)||(x>z)

Here this expression returns true if any one or both conditions are true.

!(x>y)

Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.

#include<stdio.h>
int main()
{
int a=10,b=50,c=30;
if(a>b&&a>c)
printf("a is greatest");
if(b>a&&b>c)
printf("b is greatest&quot);
if(c>a&&c>b)
printf("c is greatest");
}
/*
### Output ###
b is greatest
*/ 

👉Assignment Operators

SymbolExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
#include<stdio.h>
int main()
{
int x1=5,y1=3;
x1+=y1;   //x1=x1+y1
printf("x1=%d\n",x1);

int x2=5,y2=3;
x2-=y2;   //x2=x2-y2
printf("x2=%d\n",x2);

int x3=5,y3=3;
x3*=y3;   //x3=x3*y3
printf("x3=%d\n",x3);

int x4=5,y4=3;
x4/=y4;   //x4=x4/y4
printf("x4=%d\n",x4);

int x5=5,y5=3;
x5%=y5;   //x5=x5%y5
printf("x5=%d\n",x5);
}

### Output ###
x1=8
x2=2
x3=15
x4=1
x5=2 

👉Bitwise Operators

SymbolOperationExample
& Bitwise ANDx&y
|Bitwise ORx|y
<<Shift Leftx<<2
>>Shift Rightx>>2
#include<stdio.h>
int main()
{
	int a=5,b=3,c;//variable declaration
	c=a&b;        // AND operation
	printf("a&b=%d\n",c);
	c=a|b;        // OR operation
	printf("a|b=%d\n",c);
	c=a>>2;        // shift right operation
	printf("a>>2=%d\n",c);
	c=a<<2;        // shift left operation
	printf("a<<2=%d\n",c);
}
/*
### output ###
a&b=1
a|b=7
a>>2=1
a<<2=20
*/
For more details click here

👉Increment/Decrement Operators

SymbolNameFunctionExample
++IncrementIt increments the value by 1++x
--DecrementIt decrements the value by 1--x
#include<stdio.h>
int main()
{
int x=3,y=2;
printf("%d\n",++x);
printf("%d",--y);
}
### Output ###
4
1 

👉Conditional Operators


 
#include<stdio.h>
int main()
{
5<2?printf("Hello"):printf("Hi");
}
### Output ###
Hi//because condition is false 

👉Special Operators

SymbolDescriptionExample
&It is used find address of a variableint a=10;
printf("%d",&a);
*It is used to declare pointer type variable.int *p;
sizeof()It returns the memory size of variable.int a=10;
printf("%d",sizeof(a));

Request:-If you found this post helpful then let me know by your comment and share it with your friend.

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.

If you have not subscribed my website then please subscribe my website.

Try to learn something new and teach  something new to other.

Thanks.


Post a Comment

0 Comments