C: Printing unsigned/signed short using printf

Posted by Joys of Programming on in C/C++

Two types of integer are supported in C: signed and unsigned integers. If you want to use integer which occupies only 2 bytes, you can use short instead of int. To declare a signed short integer


signed short int varname;

To declare a unsigned short integer


unsigned short int varname;

If you do not specify a specifier with short, it is taken as signed short integer. Even int is optional.


unsigned short varname;

signed short varname;

are correct forms.

To print signed short integer, you can either use i or d both perpended by h. For unsigned integer, use u prepended by h . This is shown in the following program

#include <stdio.h>

int main()
{
 /*Unsigned Short*/
 unsigned short int uvar = 25;

 /*Signed Short*/
 signed short int svar = -25;

 /*Printing unsigned Short Integer*/
 printf ("%hu\n", uvar);

 /*Printing signed short Integer*/
 printf ("%hi\n", svar);
 printf ("%hd\n", svar);

 return 0;
}


Tags: , ,

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.