C: Printing unsigned/signed long long int using printf

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

If you want to use large integer numbers which cannot be represented using 4 bytes, you can use long long int instead of int. A long long int occupies 8 bytes. But you can make use of the sizeof operator to find the size of a long long int.

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


signed long long int varname;

To declare a unsigned long integer


unsigned long long int varname;

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


unsigned long long varname;

signed long long varname;

are correct forms.

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


#include <stdio.h>

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

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

 /*Printing size of a long long Integer*/
 printf ("Size of a long long integer: %i bytes\n", sizeof(svar));

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

 /*Printing signed long long Integer*/
 printf ("%lli\n", svar);
 printf ("%lld\n", svar);

 return 0;
}

Let’s also check the output of this program

Size of a long long integer: 8 bytes
25
-25
-25

There are certain other things which you would have noticed in the program like the suffix LL in the number 25LL. This is useful in declaring long long integers. Also note the size of long long integers

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.