C: Printing unsigned/signed long int using printf
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;
}
Comments: