Skip to main content

C Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical operation.

C language provides the following types of operators (based on operators) −

  • Arithmetic Operators
  • Increment & Decrement
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
Note: B - Binary Operator U - Unary Operator
Arithmatic Operators
Operator Name Category syntax Example
(a=10,b=3)
+ Addtion B a+b 13
- subtraction B a-b 7
* multiplication B a*b 30
division B a/b 3
% Modulus B a%b 1
  
/*

write a C program for All Arithmatic Operators:

Expected output:
================
Enter any two integers
10                                                                                                                                         
3                                                                                                                                                 
The sum of 10 and 3 is : 13
The subtraction of 10 and 3 is : 7
The multiplication of 10 and 3 is : 30
The division of 10 and 3 is : 3
The modulus of 10 and 3 is : 1

*/

#include < stdio.h >
int main()
{
    int num1, num2 ;   // which stores user given values
    printf("Enter any two integers");
    scanf("%d%d", &num1, &num2);
    
    printf("\n The sum of %d and %d is : %d",num1 ,num2, (num1+num2));
    printf("\n The subtraction of %d and %d is : %d",num1 ,num2, (num1-num2));
    printf("\n The multiplication of %d and %d is : %d",num1 ,num2, (num1*num2));
    printf("\n The division of %d and %d is : %d",num1 ,num2, (num1/num2)); // which return quotient
    printf("\n The modulus of %d and %d is : %d",num1 ,num2, (num1%num2)); // which return remainder
    
    return 0;
}
   
Try it Yourself
Relational Operators
Operator Name Category Syntax

Example

(a=10 , b-20)

< Less than B a<b

1(true)

<= lesstha or equal B a<=b

1(true)

> greater than B a>b

0 (false)

>= greater than or equal B a>=b

0 (false)

== equal B a==b

0 (false)

!= not equals B a!=b 1 (true)



/*

write a C program for All Arithmatic Operators:


Expected output:
================
Enter any two integers
10                                                                                                                                                                           
20                                                                                                                                                                                                 
                                                                                                                                                                                                   
 10 < 20 : 1                                                                                                                                                                                       
 10 <= 20 : 1                                                                                                                                                                                      
 10 > 20 : 0                                                                                                                                                                                       
 10 >= 20 : 0                                                                                                                                                                                      
 10 == 20 : 0                                                                                                                                                                                      
 10 != 20 : 1  

*/

#include<stdio.h >
int main()
{
    int num1, num2 ;  
    
    printf("Enter any two integers");
    scanf("%d%d",& num1,& num2);
    printf("\n %d < %d : %d ",num1 ,num2, (num1 < num2));
    printf("\n %d <= %d : %d",num1 ,num2, (num1 &le= num2));
    printf("\n %d > %d : %d",num1 ,num2, (num1 > num2));
    printf("\n %d >= %d : %d",num1 ,num2, (num1 >= num2));
    printf("\n %d == %d : %d",num1 ,num2, (num1 == num2));
    printf("\n %d != %d : %d",num1 ,num2, (num1 != num2));
    return 0;
}
Try it Yourself
Logical Operators
Operator Name Category syntax Example
(a=10,b=3)
&& Logical And B (a>b) &&(a<b) 0
|| Logical Or B (a>b) || (a<b) 1
! Logical Not U !(a>b) 0

/*

Write a C program for Logical operators:

Expected Output:
================

Enter any two integer85                                                                                                                                       
95                                                                                                                                                            
(a > b) << (a < b) = 0                                                                                                                                            
 (a>> b) || (a < b)= 1                                                                                                                                            
 ! (a > b )= 1

*/
#include <stdio.h> 
int main() 
{ 
    int a,b;
    printf("Enter any two integer");
    scanf("%d%d",& a,& b);
    printf("(a > b) && (a < b) = %d\n",( (a > b) && (a < b) ) ); 
    printf(" (a > b) || (a< b)= %d\n", ( (a> b) || (a < b) ));
    printf(" ! (a>b )= %d\n", (! (a>b) ));
   
  
    return 0; 
} 

Try it Yourself

Truth Table for OR, AND, XOR and NOT

X Y X|Y X&Y X^Y ~X
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0

 

Bitwise Operators
Operator Name Category syntax Example
(a=75,b=95)
& Bitwise And B a & b 75
| Bitwise Or B a|b 95
Bitwise XOR B a^b 20
~ Bitwise Not U ~a -76
<< Bitwise Leftshift B a << 3 (no of shifts) 600
>> Bitwise Rightshift B a >> 3 (no of shifts) 9
  /*

Write a C program for Bitwise operators:

Expected Output:
================

Enter any two integer75                                                                                                                                       
95                                                                                                                                                            
a& b = 75                                                                                                                                                      
a|b = 95                                                                                                                                                      
a^b = 20                                                                                                                                                      
~a = -76                                                                                                                                                      
a <<3 = 600                                                                                                                                                    
a >>3  = 9 

*/
#include <stdio.h >
int main() 
{ 
    int a,b;
    printf("Enter any two integer");
    scanf("%d%d",& a,& b);
    printf("a& b = %d\n", a & b); 
    printf("a|b = %d\n", a | b);
    printf("a^b = %d\n", a ^ b);
    printf("~a = %d\n",  ~a);
    printf("a3 = %d\n", a << 3); 
    printf("a>>3  = %d\n", a >> 3); 
  
    return 0; 
} 
  
  
Try it Yourself
Miscellenious Operators
Operator Description Example
& Returns the address of a variable. &a; returns the actual address of the variable.
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
* Pointer to a variable. *a;
? : Conditional Expression. If Condition is true ? then value X : otherwise value Y

Comments