C: Printing unsigned/signed long int using printf

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

If you want to use integer which occupies 4 bytes, you can use long instead of int. Note that such a case is useful in cases where your compiler supports only 2 bytes int. In case of gcc compilers and a normal x86 machine, both int and long int occupy 4 bytes.

Two types of integer are supported in C: signed and unsigned integers. To declare a signed long integer


signed long int varname;

To declare a unsigned long integer


unsigned long int varname;

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


unsigned long varname;

signed long varname;

are correct forms.

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

#include <stdio.h>

int main()
{
 /*Unsigned long Integer*/
 unsigned long int uvar = 25;

 /*Signed long Integer*/
 signed long int svar = -25;

 /*Printing unsigned long Integer*/
 printf ("%lu\n", uvar);

 /*Printing signed long Integer*/
 printf ("%li\n", svar);
 printf ("%ld\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.