When we create a variable, the computer needs to know what kind of value that variable will store.
For example, age might store an integer, while price might store a decimal number.
That's where data types come in.
What is a Data Type?
A data type tells the compiler what type of data a variable can store and how that data should be handled.
For example:
int age = 20;
float price = 99.50;
char grade = 'A';Here:
intstores an integer.floatstores a decimal number.charstores a single character.
Think of data types like different types of containers. You wouldn't use a water bottle to store a large amount of furniture. Similarly, the computer uses different data types for different kinds of values.
Types of Data Types in C
C data types can broadly be divided into these categories:
Category | Examples |
|---|---|
Basic / Primary |
|
Derived | Arrays, Pointers, Functions |
User-defined |
|
Void |
|
For now, let's focus on the basic data types.
1. int
int is used to store whole numbers.
int age = 20;
int marks = 85;It can store values such as:
10
25
-5
100But not decimal values like 10.5.
2. float
float is used to store decimal numbers.
float price = 99.50;
float height = 5.8;It is useful when we need fractional values.
3. double
double is also used for decimal numbers but generally provides more precision than float.
double distance = 12345.6789;So, when more precision is required, double is often preferred.
4. char
char is used to store a single character.
char grade = 'A';A character is written inside single quotes.
Examples:
char letter = 'B';
char symbol = '#';
char digit = '5';Remember that '5' is a character, while 5 is an integer.
5. void
void means no value or no type.
It is commonly used with functions that don't return a value.
For example:
void display()
{
printf("Hello");
}We'll understand void properly when we learn functions.
Common Data Types at a Glance
Data Type | Used For | Example |
|---|---|---|
| Whole numbers |
|
| Decimal numbers |
|
| More precise decimals |
|
| Single character |
|
| No value/type |
|
The exact size and range of some C data types can depend on the compiler and platform, so don't memorize a fixed size for every type. The C standard only guarantees minimum ranges: for example, short and int must be at least 16 bits, long at least 32 bits, and long long at least 64 bits.
Typical Sizes and Type Modifiers
C also has type modifiers that change the size or sign of the basic types: short, long, signed, and unsigned. On most modern 64-bit compilers, the sizes are:
Type | Typical Size | Typical Range / Notes |
|---|---|---|
| 1 byte | Always exactly 1 byte by definition |
| 2 bytes | -32,768 to 32,767 |
| 4 bytes | -2,147,483,648 to 2,147,483,647 |
| 4 bytes | 0 to 4,294,967,295 |
| 8 bytes on 64-bit Linux/macOS, 4 bytes on Windows | Depends on the platform |
| 8 bytes | At least 64 bits |
| 4 bytes | About 6-7 significant digits |
| 8 bytes | About 15-16 significant digits |
You can check the size of any type on your own system with the sizeof operator:
printf("%zu\n", sizeof(int)); // usually prints 4
Simple Example
#include <stdio.h>
int main()
{
int age = 20;
float height = 5.8;
char grade = 'A';
printf("%d\n", age);
printf("%f\n", height);
printf("%c\n", grade);
return 0;
}Output:
20
5.800000
A
Here, each variable uses a different data type because each stores a different kind of value. Notice that %f prints 6 digits after the decimal point by default, so 5.8 appears as 5.800000.
In Simple Words
A data type tells C what kind of data a variable is going to store.
For example:
20 → int
5.8 → float
'A' → char
3.14159 → doubleUnderstanding data types is important because almost every C program you write will use variables to store some kind of data.