Home Segments Index Top Previous Next

152: Mainline

Note that you must have a program perform a cast, of the sort you learned about in Segment 73, if you want the program to compare an integer with a floating-point number. The following program illustrates how such comparisons must be performed:

#include   
main ( ) { 
  int i = 50; 
  double d = 50.0; 
  printf ("i == (int) d    yields %i\n", (i == (int) d)); 
  printf ("(double) i != d yields %i\n", ((double) i != d)); 
  printf ("i > (int) d     yields %i\n", (i > (int) d))    ; 
  printf ("(double) i < d  yields %i\n", ((double) i < d)) ; 
  printf ("i >= (int) d    yields %i\n", (i >= (int) d))   ; 
  printf ("(double) i <= d yields %i\n", ((double) i <= d)); 
} 
--- Result --- 
i == (int) d    yields 1 
(double) i != d yields 0 
i > (int) d     yields 0 
(double) i < d  yields 0 
i >= (int) d    yields 1 
(double) i <= d yields 1