·  C – Syntax – Variables – long

 

- Info:

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

      long     value    = 'A';

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

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

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

   

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

      {

        char   convert1 = value;

        short  convert2 = value;

        int    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("%ld %li  \n", -65, -65    ); //-65 -65   Print signed   long as decimal.

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

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

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

      printf("%lX %lx  \n", value, value); //41 41     Print unsigned long as hexadecimal.

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

   

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

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

   

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

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

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

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

   

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

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

   

      return 0;

 

    }