Data Types in C
A data type defines what kind of data a variable can store. It helps the compiler understand how much memory to allocate and what operations are allowed on that data. Think of data types like different containers — a water bottle stores liquids, a box stores solids. Similarly,int stores whole numbers, float stores decimals, and char stores characters. Choosing the right data type means efficient memory use.
Categories of Data Types
| Category | Description | Examples |
|---|---|---|
| Primitive | Basic built-in types | int, char, float, double |
| Derived | Built from primitive types | array, pointer, function |
| User-Defined | Defined by the programmer | struct, union, enum |
Primitive Data Types
Integer (int)
- Stores whole numbers (positive, negative, or zero)
- Can be
signed(default) orunsigned - Size: 4 bytes
- Range (signed): −2,147,483,648 to 2,147,483,647
- Range (unsigned): 0 to 4,294,967,295
- Signed: min = −2^(n−1), max = 2^(n−1) − 1
- Unsigned: min = 0, max = 2^n − 1
Character (char)
- Stores a single character enclosed in single quotes:
'A','z','3' - Internally stored as its ASCII value (an integer)
- Size: 1 byte
- Range (signed): −128 to 127 | Range (unsigned): 0 to 255
char:
- Attendance:
'P'for Present,'A'for Absent - Voting:
'Y'for Yes,'N'for No - Product sizes:
'S','M','L'
Float (float)
- Stores decimal numbers with single precision (~6–7 significant digits)
- Size: 4 bytes | Range: 1.2E−38 to 3.4E+38
- Used when decimal values are needed but high precision isn’t critical
Double (double)
- Stores decimal numbers with double precision (~15–16 significant digits)
- Size: 8 bytes | Range: 1.7E−308 to 1.7E+308
long doubleprovides even higher precision (10–16 bytes)
double:
| Domain | Use Case |
|---|---|
| Banking Software | Accurate money calculations |
| GPS / Navigation | High-precision latitude and longitude |
| Scientific Instruments | Measuring small/large physical values |
| Medical Devices | Dosage and vital sign measurement |
Void (void)
void represents no value. It has three main uses:
- No return type:
void display()— function returns nothing - Void pointer:
void *ptr— can hold address of any type - No parameters:
int main(void)— function takes no arguments
Data Modifiers
Modifiers adjust the size or sign of a base data type:| Modifier | Effect |
|---|---|
short | Reduces size of int to 2 bytes |
long | Increases size of int or double |
signed | Allows positive and negative values (default) |
unsigned | Allows only non-negative values; doubles the max range |
Non-Primitive Data Types
| Category | Type | Description |
|---|---|---|
| Derived | Array | Same-type elements |
| Derived | Pointer | Address of a variable |
| Derived | Function | Reusable code block |
| User-Defined | Struct | Groups different types |
| User-Defined | Union | Shared memory members |
| User-Defined | Enum | Named integer constants |
| User-Defined | Typedef | Alias for a type |