home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch3 / floatpt.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  59 lines

  1. /*                            floatpt.c
  2.  *
  3.  *   Synopsis  - The values of variables of type float, double,
  4.  *               and long double are initialized and displayed
  5.  *               in different formats by printf().  The sizeof()
  6.  *               each type is also displayed.
  7.  *
  8.  *   Objective - Illustrates declaration, assignment, and
  9.  *               some of the output options with printf()
  10.  *               of floating point variables.
  11.  */
  12.  
  13. /* Include Files */
  14. #include <stdio.h>
  15.  
  16. int main( void )
  17. {
  18.      float       floatvar;                            /* Note 1 */
  19.      double      doublevar;
  20.      long double ldvar;
  21.                                                       /* Note 2 */
  22.      printf( "The number of bytes in a float is %d.\n",
  23.                                           sizeof( float ) );
  24.      printf( "The number of bytes in a double is %d.\n",
  25.                                           sizeof( double ) );
  26.      printf( "The number of bytes in a long double is %d.\n",
  27.                                           sizeof( long double ) );
  28.  
  29.      floatvar = 65.328f;                             /* Note 3 */
  30.                                                      /* Note 4 */
  31.      printf( "floatvar has the value %7.2f.\n", floatvar );
  32.      printf( "It can also be written in the form %10.3e.\n",
  33.                                           floatvar );
  34.      printf( "It can also be written in the form %10.3E.\n",
  35.                                           floatvar );
  36.      printf( "It can also be written in the form %10.3g.\n",
  37.                                           floatvar );
  38.  
  39.      doublevar = 1.2465e-5;                         /* Note 5 */
  40.      printf( "doublevar has the value %6.3f.\n", doublevar );
  41.      printf( "It can also be written in the form %7.5e.\n",
  42.                                           doublevar );
  43.      printf( "It can also be written in the form %7.5E.\n",
  44.                                           doublevar );
  45.      printf( "It can also be written in the form %7.5g.\n",
  46.                                           doublevar );
  47.  
  48.      ldvar = 584.365E+17L;                         /* Note 6 */
  49.                                                    /* Note 7 */
  50.      printf( "ldvar has the value %7.2Lf.\n", ldvar );
  51.      printf( "It can also be written in the form %10.3Le.\n",
  52.                                           ldvar );
  53.      printf( "It can also be written in the form %10.3LE.\n",
  54.                                           ldvar );
  55.      printf( "It can also be written in the form %10.3Lg.\n",
  56.                                           ldvar );
  57.      return 0;
  58. }
  59.