·  C – Syntax – Variables – float

 

- Info:

  - This tutorial shows how to create float and convert it to other Primitive Types.

  - More on printf() function can be found st http://www.cplusplus.com/reference/clibrary/cstdio/printf/.

 

- Procedure:

  - Create C Console Application Test.c.

   

    Test.c

    #include <stdio.h>

   

    int main () {

   

      /*CREATE.------------------------------------------------------------------------*/

      float    value    = 'A';

               value    =  65;    /* Decimal     ASCII value. */

               value    = 0101;   /* Octal       ASCII value. */

               value    = 0x41;   /* Hexadecimal ASCII value. */

               value    = 34.55/* 34.549999                */

   

      /*CONVERT.-----------------------------------------------------------------------*/

      {

        char   convert1 = value;  /* 34        */

        short  convert2 = value;  /* 34        */

        int    convert3 = value;  /* 34        */

        long   convert4 = value;  /* 34        */

        double convert5 = value;  /* 34.549999 */

        printf("%c %i %f\n", convert1, convert4, convert5); /* " 34 34.549999 */

      }

   

      /*DISPLAY.-----------------------------------------------------------------------*/

      printf("%f      \n", value); //34.549999     Use floating point notation.

      printf("%.3f    \n", value); //34.550        Round it on 3 decimal places.

      printf("%7.3f   \n", value); // 34.550       Right justify with spaces using 7 spaces.

      printf("%07.3f  \n", value); //034.550       Right justify with zeros  using 7 spaces.

      printf("%-7.3f< \n", value); //34.550 <      Left justify with spaces  using 7 spaces.

   

      printf("%e      \n", value); //3.455000e+001 Use scientific notation with e.

      printf("%E      \n", value); //3.455000E+001 Use scientific notation with E.

      printf("%.3E    \n", value); //3.455E+001    Round it on 3 decimal places.

      printf("%12.3E  \n", value); //  3.455E+001  Right justify with spaces using 12 spaces.

      printf("%012.3E \n", value); //003.455E+001  Right justify with zeros  using 12 spaces.

      printf("%-12.3E<\n", value); //3.455E+001  < Left justify with spaces using 12 spaces.

   

      printf("%g      \n", value); //34.55         Use shorter of %f or %e.

      printf("%G      \n", value); //34.55         Use shorter of %f or %E.

   

      return 0;

 

    }