C Integer Types

Summary: in this tutorial, you will learn about C integer types and understand how the signed / unsigned, short / long qualifiers work.

Introduction to the C integer types

Integer numbers are whole numbers including negative, zero, and positive numbers, for example, -1, 0, 1, 2 … 2020. Integer numbers have no decimal point. For example, the number 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)

To store integers internally, C uses a fixed number of bits (a sequence of 0 and 1). 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 or not.

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 TypesSynonymsNotes
intsigned, signed int 
shortshort int, signed short, signed short int 
longlong int, signed long, signed long int 
long longlong long int, signed long long, signed long long intAvailable 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 Typesunsigned Integer Types
intunsigned int
shortunsigned short
longunsigned long
long longunsigned 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:

TypeStorage sizeMinimum valueMaximum value
char1 byte-128127
unsigned char1 byte0255
signed char1 byte-128127
int2 bytes or 4 bytes-32,768 or -2,147,483,64832,767 or 2,147,483,647
unsigned int2 bytes or 4 bytes065,535 or 2,147,483,647
short2 bytes-32,76832,767
unsigned short2 bytes065,535
long4 bytes-2,147,483,6482,147,483,647
unsigned long4 bytes04,294,967,295
long long(C99)8 bytes-9,223,372,036, 854,775,8089,223,372,036,854,775,807
unsigned long long8 bytes018,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 int keyword to represent integer type.
  • The size of the integers depends on the platform where the program runs.
  • The limits.h has the INT_MIN and INT_MAX that specify the minimum and maximum integer values.
  • Apply the signed and unsigned qualifier to an integer type to declare signed and unsigned integers.
  • Apply the short and long qualifier 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.
Was this tutorial helpful ?