Summary: in this tutorial, you will learn various C integer types including signed and unsigned integers.
C provides you with five signed integer types. Each integer type has several synonyms.
The following table illustrates the first five integer types with their corresponding synonyms:
|int||signed, signed int|
|short||short int, signed short, signed short int|
|long||long int, signed long, signed long int|
|long long||long long int, signed long long, signed long long int||Available since C99|
C unsigned integer types
For each signed integer, C also provides the corresponding unsigned integer type that has the same memory size as the signed integer type.
The following table illustrates the unsigned integer type:
|Signed Integer Types||unsigned Integer Types|
|long long||unsigned long long|
C integer types value ranges
C defines exactly minimum storage size of each integer type e.g.,
short takes at least two byes,
long takes at least 4 bytes. Regardless of the C’s implementation, the size of integer types must follows the order below:
sizeof(short) < sizeof(int) < sizeof(long) < sizeof(long long)
The following table gives you the common sizes of the integer types in C:
|Type||Storage size||Minimum value||Maximum value|
|unsigned char||1 byte||0||255|
|signed char||1 byte||-128||127|
|int||2 bytes or 4 bytes||-32,768 or -2,147,483,648||32,767 or 2,147,483,647|
|unsigned int||2 bytes or 4 bytes||0||65,535 or 2,147,483,647|
|unsigned short||2 bytes||0||65,535|
|unsigned long||4 bytes||0||4,294,967,295|
|long long(C99)||8 bytes||-9,223,372,036, 854,775,808||9,223,372,036, 854,775,807|
|unsigned long long||8 bytes||0||18,446,744,073, 709,551,615|
The value ranges of integer types can be found in the
limits.h header file. This header file contains the macros that define minimum and maximum values of each integer type e.g.,
INT_MAX for minimum and maximum size of the integer.
Getting the sizes of integer types
To obtain the size of an integer type, or any type, you use the
sizeof() operator. The following program demonstrates how to use the
sizeof() operator to get the sizes of various integer types in your system.
* File : main.c
* Author : zentut.com
* Purpose: display sizes of integer types
printf("sizeof(short) = %d bytes\n",sizeof(short));
printf("sizeof(int) = %d bytes\n",sizeof(int));
printf("sizeof(unsigned int) = %d bytes\n",sizeof(unsigned int));
printf("sizeof(long) = %d bytes\n",sizeof(long));
The following is the output in our system:
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(unsigned int) = 4 bytes
sizeof(long) = 4 bytes
In this tutorial, we have introduced various C integer types and shown you how to use the
sizeof() operator to get the sizes of an integer types in your system.