The comparison operators  (==, !=, <, <=, >, >=)  in C

  • The comparison operators in C are the same one found in Java....

  • Except that:

      • The comparison operators will return 1 when the result is true and return 0 otherwise

  • Summary of the comparison operators in C:

       Operator | Meaning
      ----------+-----------------------------------------------------   
       x == y   | returns 1 if x is equals to y, returns 0 otherwise
       x != y   | returns 1 if x is not equals to y, returns 0 otherwise 
       x <  y   | Ditto: less than
       x <= y   | Ditto: less than or equal
       x >  y   | Ditto: greater than
       x >= y   | Ditto: greater than or equal
      

The logic operators   (!, &&, ||)   in C

  • The logic operators in C are the same one found in Java....

  • Except that:

      • The comparison operators operators on numerical values, where:

           0false
           any non-zero valuetrue

      • The logic operators will return 1 when the result is true and return 0 otherwise

  • Summary of the logic operators in C:

       Operator | Meaning
      ----------+-----------------------------------------------------   
       x && y   | returns 1 if "x and y" is true, returns 0 otherwise
       x || y   | Ditto: logical or
       ! x      | Ditto: logical not 

Example C program showing the effect of the comparison and logical operators in C

 

#include <stdio.h>

int main( int argc, char* argv[] )
{
   printf("4 == 3 = %d\n",  (4==3)  );   // Prints 0               
   printf("4 > 3 = %d\n",   (4 > 3) );   // Prints 1
 
   printf("4 && 3 = %d\n",  (4 && 3) );  // true  && true  --> 1
   printf("4 && 0 = %d\n",  (4 && 0) );  // true  && false --> 0
   printf("4 || 0 = %d\n",  (4 || 0) );  // true  || false --> 1
   printf("0 || 0 = %d\n",  (0 || 0) );  // false || false --> 0
}
  

DEMO: demo/C/set1/logic2.c