Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

C – Operators

C Arithmetic operators

C Programming Arithmetic Operators

Example

 #include <stdio.h>
 int main()
 {
     int a = 11;
     int b = 5;
     int ans;
	
     ans = a+b; // addition 
     printf("\n Addition  = %d",ans);
	
     ans = a-b; // subtraction 
     printf("\n Subtraction  = %d",ans);
	
     ans = a*b; // multiplication 
     printf("\n Multiplication  = %d",ans);
	
     ans = a/b; // division 
     printf("\n Division  = %d",ans);
	
     ans = a%b; // modulus 
     printf("\n Remainder  = %d",ans);
	
     return 0;
 }

Output

Addition = 16
Subtraction = 6
Multiplication = 55
Division = 2
Remainder = 1

Increment & Decrement operators

C Programming Language Increment & decrement OperatorsExampleC Programming Language Increment & decrement Operators Examples

Example for ++ x

 #include <stdio.h>
 int main()
 {
     int x = 5;
     int y;
     printf("\n Before increment");
     printf("\n x = %d",x);	
     y = ++x;	
     printf("\n After increment");
     printf("\n x = %d",x);
     printf("\n y = %d",y);
	
     return 0;
 }

Output

Before increment
x = 5
After increment
x = 6
y = 6

Example for x++

 #include <stdio.h>
 int main()
 {
     int x = 5;
     int y;
     printf("\n Before increment");
     printf("\n x = %d",x);	
     y = x++;	
     printf("\n After increment");
     printf("\n x = %d",x);
     printf("\n y = %d",y);
	
     return 0;
 }

Output

Before increment
x = 5
After increment
x = 6
y = 5

Example for – -x

 #include <stdio.h>
 int main()
 {
     int x = 5;
     int y;
     printf("\n Before decrement");
     printf("\n x = %d",x);	
     y = --x;	
     printf("\n After decrement");
     printf("\n x = %d",x);
     printf("\n y = %d",y);
	
     return 0;
 }

Output

Before decrement
x = 5
After decrement
x = 4
y = 4

Example for x- –

 #include <stdio.h>
 int main()
 {
     int x = 5;
     int y;
     printf("\n Before decrement");
     printf("\n x = %d",x);
     y = x--;
     printf("\n After decrement");
     printf("\n x = %d",x);
     printf("\n y = %d",y);
	
     return 0;
 }

Output

Before decrement
x = 5
After decrement
x = 4
y = 5

C assignment operators

C Programming Language Assignment OperatorsC Programming Language Assignment Operators Examples

Example

 #include <stdio.h>
 int main()
 {
     int x = 10;
     int y = 5;
	
     y += x;  // y = y+x
     printf("\n y = %d",y);
	
     y -= x; // y = y-x
     printf("\n y = %d",y);
	
     y *= x; // y = y*x
     printf("\n y = %d",y);
	
     y /= x; // y = y/x
     printf("\n y = %d",y);
	
     y %= x; // y = y%x
     printf("\n y = %d",y);
	
     return 0;
 }

Output

y = 15
y = 5
y = 50
y = 5
y = 5

Relation operators

C Programming Language Relation Operators
If the relation is false, it returns 0 (Zero).
If the relation is true, it returns 1.

Example

#include <stdio.h>
int main()
{
	int x = 5;
	int y = 10;
	
	printf("\n x == y returns %d",x==y);
	printf("\n x < y returns %d",x<y);
	printf("\n x > y returns %d",x>y);
	printf("\n x <= y returns %d",x<=y);
	printf("\n x >= y returns %d",x>=y);
	printf("\n x != y returns %d",x!=y);
	
	printf("\n\n 1 = true");
	printf("\n\n 0 = false");
	return 0;
}

Output

 x == y returns 0
 x < y returns 1 x > y returns 0
 x <= y returns 1 x >= y returns 0
 x != y returns 1

 1 = true

 0 = false

Logical or Boolean operators

C Programming Language Logical or Boolean Operators

Example

#include <stdio.h>
int main()
{
	int a=10, b=5, c=20, d=0;
	
	printf("\n\n (a>b) && (b<c) = %d",(a>b) && (b<c));
	printf("\n (a>b) && (b>c) = %d",(a>b) && (b>c));

	printf("\n\n (a>b) || (b>c) = %d",(a>b) || (b>c));
	printf("\n (a<b) || (b>c) = %d",(a<b) || (b>c));
	
	printf("\n\n !1 = %d",!1);
	printf("\n !b = %d",!b);
	printf("\n !d = %d",!d);
	return 0;
}

Output

Bitwise operators
These operators operate on bit to perform bit by bit operation.

C Programming Language Bitwise Operators

The sizeof operator

The sizeof operator is used to check the size of operand in bytes.
The operand can be of any type like data-type, constant or variable.

Example

sizeof(int)
sizeof(char)

Example

 #include <stdio.h>
 int main()
 {
     int a = 5;
     printf("\n sizeof(int) = %d",sizeof(int));
     printf("\n sizeof(float) = %d",sizeof(float));
     printf("\n sizeof(char) = %d",sizeof(char));
     printf("\n sizeof(double) = %d",sizeof(double));
     printf("\n sizeof(a) = %d",sizeof(a));
     printf("\n sizeof(10) = %d",sizeof(10));
     printf("\n sizeof(5.66) = %d",sizeof(5.66));
	
     return 0;
 }

Output

sizeof(int) = 4
sizeof(float) = 4
sizeof(char) = 1
sizeof(double) = 8
sizeof(a) = 4
sizeof(10) = 4
sizeof(5.66) = 8

The Comma Operator

It is used to bring same type expressions together.

Example

int a, b, c;

In the above example a, b & c are of the same type. They are brought together by comma operator.

 

Leave a reply