Relational Operators In C

Relational Operators in C programming language return true(non-zero number) or false(0) value. They operate on 2 operands.

Relational Operators

== equality operator.
!= inequality operator.
> greater than operator.
< less than operator.
>= greater than or equal to operator.
<= less than or equal to operator.

== equality operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 10, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a == b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 1

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 10, b = 20;  
  9.     int temp;  
  10.   
  11.     temp = (a == b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 0

!= inequality operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 10, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a != b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 0

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 10, b = 20;  
  9.     int temp;  
  10.   
  11.     temp = (a != b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 1

> operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 20, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a > b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 1

< operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 20, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a < b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 0

>= operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 20, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a >= b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 1

<= operator

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     //Relational Operators  
  7.   
  8.     int a = 20, b = 10;  
  9.     int temp;  
  10.   
  11.     temp = (a <= b);  
  12.   
  13.     printf("value of temp is %d\n", temp);  
  14.   
  15.     return 0;  
  16. }  

Output:
value of temp is 0

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Relational Operators In C



YouTube Link: https://www.youtube.com/watch?v=8wlJ4bo110Y [Watch the Video In Full Screen.]


Note: = is assignment operator. == is equality operator.

This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert