C programming language supports various datatypes to handle different kinds of data. These datatypes can be broadly classified into three main categories: Integers, Floats, and Characters.
Integer datatypes are used to store whole numbers. The size and range of an integer datatype depend on the system and the compiler. Here are the commonly used integer datatypes:
Type | Size (in bytes) | Range |
---|---|---|
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 int |
4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int |
4 | 0 to 4,294,967,295 |
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 |
Floating-point datatypes are used to store real numbers (numbers with fractional parts). The following are the standard floating-point datatypes in C:
Type | Size (in bytes) | Range | Precision |
---|---|---|---|
float |
4 | ~1.2E-38 to ~3.4E+38 | 6 decimal places |
double |
8 | ~2.3E-308 to ~1.7E+308 | 15 decimal places |
long double |
16 | ~3.4E-4932 to ~1.1E+4932 | 19 decimal places |
The character datatype is used to store single characters. It typically requires 1 byte of memory. Here are the details:
Type | Size (in bytes) | Range |
---|---|---|
char |
1 | -128 to 127 |
unsigned char |
1 | 0 to 255 |
Here are some examples of how these datatypes can be declared and used in a C program:
Here in this code, we see data of different data types is being stored in different variables and then printed on the console.
NOTE: %d, %f, %c
etc are known as control strings. We'll discuss them better in Inputs and Outputs