Data type is used for declaring the type of a variable. In C programming, data types are used to determine the type and size of data associated with a variable. It is important that the programmer decides the data type of a variable before storing any value.
Integer Data Types
| Data Type | Memory Size (Bytes) | Range |
|---|---|---|
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | 0 to 4,294,967,295 |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long | 8 | 0 to 18,446,744,073,709,551,615 |
long long int | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
The above table represents the different types of integer data types, their memory size and the range of values they hold. Something to note here is that these datatypes and their memory size can vary between compiler designs. For example, the above is for gcc. There are other compilers like XC8 which has integer data types as below:
| Data Type | Size in Bits | Data Range / Usage |
|---|---|---|
unsigned char | 8-bit | 0 to 255 |
char | 8-bit | -128 to +127 |
unsigned int | 16-bit | 0 to 65,535 |
int | 16-bit | -32,768 to +32,767 |
unsigned short | 16-bit | 0 to 65,535 |
short | 16-bit | -32,768 to +32,767 |
unsigned short long | 24-bit | 0 to 16,777,215 |
short long | 24-bit | -8,388,608 to +8,388,607 |
unsigned long | 32-bit | 0 to 4,294,967,295 |
long | 32-bit | -2,147,483,648 to +2,147,483,647 |
You can notice that there are different data types like short long, which is 3 bytes long. Similarly, below is another one from ARMCC:
| Type | Size in Bits | Natural Alignment in Bytes | Range of Values |
|---|---|---|---|
char | 8 | 1 (byte-aligned) | 0 to 255 (unsigned) by default; -128 to 127 (signed) when compiled with --signed_chars |
signed char | 8 | 1 (byte-aligned) | -128 to 127 |
unsigned char | 8 | 1 (byte-aligned) | 0 to 255 |
(signed) short | 16 | 2 (halfword-aligned) | -32,768 to 32,767 |
unsigned short | 16 | 2 (halfword-aligned) | 0 to 65,535 |
(signed) int | 32 | 4 (word-aligned) | -2,147,483,648 to 2,147,483,647 |
unsigned int | 32 | 4 (word-aligned) | 0 to 4,294,967,295 |
(signed) long | 32 | 4 (word-aligned) | -2,147,483,648 to 2,147,483,647 |
unsigned long | 32 | 4 (word-aligned) | 0 to 4,294,967,295 |
(signed) long long | 64 | 8 (doubleword-aligned) | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long long | 64 | 8 (doubleword-aligned) | 0 to 18,446,744,073,709,551,615 |
From all the above example, we can see that the integer data types differ across compilers. It is upto the compiler design to allocate different memory size and range of values. So it is important to understand the compiler that is being used.
Note
The C Standard does not specify the size of data types. They only give a range. For example, the standard can say that a
longtype can have a size between 32 and 64 bit long. Then the compiler designers can pick either a 32bit version, or a 64 bit version or different sizes in between. Int And Long Data Types shows what this means forintandlongin practice.
Type By Type Notes
Each integer type is covered in its own note, one per lecture:
- Char Data Type: 1 byte, stores a single character, and how its range is derived
- Short Int Data Type: 2 bytes, and its signed and unsigned ranges
- Int And Long Data Types: why the compiler decides if they are 2 or 4 bytes, and 4 or 8 bytes
More types will be added here as the lectures cover them.
Previous: Escape Sequences