·  C – Syntax – Variables – int

 

- Info:

  - This tutorial shows how to create int 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.------------------------------------------------------------------------*/

      int      value    = 'A';

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

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

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

   

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

      {

        char   convert1 = value;

        short  convert2 = value;

        long   convert3 = value;

        float  convert4 = value;

        double convert5 = value;

        printf("%c %i %f\n", convert1, convert2, convert5);

      }

 

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

      printf("%c     \n", value       ); //A         Print ASCII character.

 

      printf("%d %i  \n", -65, -65    ); //-65 -65   Print signed   integer as decimal.

      printf("%u     \n", value       ); //65        Print unsigned integer as decimal.

      printf("%o     \n", value       ); //101       Print unsigned integer as octal.

      printf("%#o    \n", value       ); //0101      Print unsigned integer as octal       with 0 prexif

      printf("%X  %x \n", value, value); //41 41     Print unsigned integer as hexadecimal.

      printf("%#X %#x\n", value, value); //0X41 0x41 Print unsigned integer as hexadecimal with 0X or 0x

   

      printf("%+d    \n", value       ); //+65       Print + sign before positive numbers.

      printf("% d    \n", value       ); //+65       Print space  before positive numbers.

   

      printf("%4d    \n", -12         ); // -12      Right justify with spaces.

      printf("%04d   \n", -12         ); //-012      Right justify with zeros.

      printf("%04d   \n", value       ); //0065      Right justify with zeros.

      printf("%4d    \n", -1234       ); //-1234     If not enough space number is not truncated.

   

      printf("%-4d<  \n", -12         ); //-12 <     Left justify with spaces. Zeros are not supported.

      printf("%-4d<  \n", -1234       ); //-1234<    If not enough space number is not truncated.

 

      return 0;

 

    }