Learn C - C Comparison Operators






You may determine equality or difference among variables or values.

You have six relational operators that you use to compare two values, as shown in the following Table.

OperatorMeaning
==is equal to
!=is not equal
>is greater than
<is less than
>=is greater than or equal to
<=is less than or equal to

Each of these operations results in a value of type int.

The result of each operation is 1 if the comparison is true and 0 if the comparison is false.





Example

This is sample code for comparison usage


  #include <stdio.h> 
/*from   w w w.  j  a  v  a2s. c  om*/
  int main() { 
     int a,b; 
     a = 10; 
     b = 6; 
      
     printf("%d == %d = %d\n",a,b,a==b); 
     printf("%d  != %d = %d\n",a,b,a!=b); 
     printf("%d > %d = %d\n",a,b,a>b); 
     printf("%d < %d = %d\n",a,b,a<b); 
     printf("%d >= %d = %d\n",a,b,a>=b); 
     printf("%d <= %d = %d\n",a,b,a<=b); 

     return 0; 
  } 

The code above generates the following result.