Summary: In this tutorial, you will learn about C integer types and understand how the signed / unsigned and short/long qualifiers work.
Introduction to the C integer types #
Integer numbers are whole numbers, including negative, zero, and positive numbers, such as -1, 0, 1, 2, and 2020. They have no decimal point. For example, 3.14 is not an integer because it contains a decimal point.
C uses the int keyword to represent the integer type. The following declares a variable with the integer type:
int age = 1;Code language: C++ (cpp)C uses a fixed number of bits (a sequence of 0 and 1) to store integers internally. The number of bits also changes from one computer to another.
For example, most UNIX machines use 32 bits (4 bytes) to represent integers. So the range of the int numbers is from -232 (-2,147,483,648) to 231-1 (2,147,483,647).
However, some legacy PC use 16 bits to represent integers. Therefore, the range of the integers is from -32,768 to 32,767.
The limits.h file defines two values that represent the minimum and maximum integers. The following program displays the integer range on your computer:
#include <stdio.h>
#include <limits.h>
int main()
{
printf("(%d, %d)", INT_MIN, INT_MAX);
return 0;
}Code language: C++ (cpp)Short / Long qualifiers #
C provides you with two qualifiers short and long that change the size of the integers. Typically, short is often 16 bits, and long is at least 32 bits.
The rule is that short is not longer than int, and int is not longer than long. However, this depends on whether the compilers will respect the rule.
Signed / unsigned integers #
C provides two qualifiers called signed and unsigned that apply to any integer. Unsigned integers are always positive and zero. For example:
unsigned int quanity = 20;
signed int profit = 0;Code language: C++ (cpp)The following table illustrates integer types with their corresponding synonyms:
| Integer Types | Synonyms | Notes |
|---|---|---|
| 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 also provides the corresponding unsigned integer type with the same memory size for each signed integer type. The following table illustrates the unsigned integer types:
| Signed Integer Types | unsigned Integer Types |
|---|---|
| int | unsigned int |
| short | unsigned short |
| long | unsigned long |
| long long | unsigned long long |
Ranges of integers #
C defines exactly the minimum storage size of each integer type, e.g., the short takes at least 2 byes, long takes at least 4 bytes.
The following table shows the common sizes of integer types in C:
| Type | Storage size | Minimum value | Maximum value |
|---|---|---|---|
| char | 1 byte | -128 | 127 |
| 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 |
| short | 2 bytes | -32,768 | 32,767 |
| unsigned short | 2 bytes | 0 | 65,535 |
| long | 4 bytes | -2,147,483,648 | 2,147,483,647 |
| 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 |
Getting the sizes of integer types #
To get the size of an integer type, you use the sizeof() operator that returns the size of a type in bytes. For example, the following program uses the sizeof() operator to get the sizes of various integer types:
#include <stdio.h>
int main()
{
printf("sizeof(short) = %d bytes\n",sizeof(short));
printf("sizeof(int) = %d bytes\n",sizeof(int));
printf("sizeof(signed int) = %d bytes\n",sizeof(signed int));
printf("sizeof(long) = %d bytes\n",sizeof(long));
printf("sizeof(long long) = %d bytes\n",sizeof(long long));
return 0;
}Code language: C++ (cpp)Summary #
- Integers are whole numbers, including negative, 0, and positive.
- C uses the
intkeyword to represent integer type. - The size of the integers depends on the platform where the program runs.
- The
limits.hhas theINT_MINandINT_MAXthat specify the minimum and maximum integer values. - Apply the
signedandunsignedqualifier to an integer type to declaresignedandunsignedintegers. - Apply the
shortandlongqualifier to an integer type to change the size of an integer type. - Use the
sizeof()operator to get the size of an integer type in bytes.