home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap03 / mixed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-06  |  1.2 KB  |  37 lines

  1. /* mixed.c -- shows the effects of mixing */
  2. /*            data types in an expression */
  3.  
  4. main()
  5. {
  6.     int     i = 50, iresult;
  7.     long    l = 1000000, lresult;
  8.     float   f = 10.5, fresult;
  9.     double  d = 1000.005, dresult;
  10.  
  11.     fresult = i + f;         /* int + float to float */
  12.     printf("%f\n", fresult);
  13.  
  14.     fresult = i * f;         /* int * float to float */
  15.     printf("%f\n", fresult);
  16.  
  17.     lresult = l + f;         /* long + int to long */
  18.     printf("%f\n", lresult);
  19.  
  20.     printf("%f\n", d * f);   /* double * float */
  21.                                  /* to double */
  22.     fresult = d * f;         /* assigned to a float */
  23.     printf("%f\n", fresult); /* loses some precision */
  24.  
  25.     /* debugging a division problem */
  26.  
  27.     iresult = i / l;          /* int / long to int */
  28.     printf("%d\n", iresult);  /* whoops! loses result */
  29.     printf("%ld\n", iresult); /* this won't fix it */
  30.     fresult = i / l;          /* store in float result */
  31.     printf("%f\n", fresult);  /* doesn't work */
  32.     dresult = i / l;          /* try a double */
  33.     printf("%f\n", dresult);  /* doesn't work */
  34.     fresult = (float) i / l;  /* try type cast */
  35.     printf("%f\n", fresult);  /* correct result */
  36. }
  37.