·  C – Syntax – Variables – short

 

- Info:

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

      short    value    = 'A';

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

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

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

   

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

      {

        char   convert1 = value;

        int    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  as ASCII character.

   

      printf("%hd %hi  \n", -65, -65    ); //-65 -65   Print signed   short as decimal.

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

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

      printf("%#ho     \n", value       ); //0101      Print unsigned short as octal       with 0 prefix

      printf("%hX %hx  \n", value, value); //41 41     Print unsigned short as hexadecimal.

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

   

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

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

   

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

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

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

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

   

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

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

   

      return 0;

    }