Data Types in C
In C programming, a data type defines the kind of value a variable can store. Every variable must have a data type declared before it can be used. The data type tells the compiler how much memory to allocate and what kind of operations can be performed on that variable.
Think of a data type like a container — a glass holds water, a box holds books, and a bag holds clothes. Each container is designed for a specific type of content. Similarly, each data type in C is designed to store a specific kind of data.
Classification of Data Types in C
Data types in C are divided into four main categories:
- Basic (Primary) Data Types
- Derived Data Types
- User-Defined Data Types
- Void Data Type
Data Types in C
│
├── Basic (Primary)
│ ├── int
│ ├── float
│ ├── double
│ └── char
│
├── Derived
│ ├── Array
│ ├── Pointer
│ └── Function
│
├── User-Defined
│ ├── struct
│ ├── union
│ └── enum
│
└── Void
└── void
Basic Data Types
These are the fundamental data types built into the C language.
1. int — Integer Type
Used to store whole numbers (numbers without decimal points). This includes both positive and negative numbers.
| Type | Size (Typical) | Range |
|---|---|---|
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| short int | 2 bytes | -32,768 to 32,767 |
| long int | 4 or 8 bytes | -2,147,483,648 to 2,147,483,647 (or larger) |
| unsigned int | 4 bytes | 0 to 4,294,967,295 |
#include <stdio.h>
int main()
{
int apples = 10;
int temperature = -5;
unsigned int students = 350;
printf("Apples: %d\n", apples);
printf("Temperature: %d\n", temperature);
printf("Students: %u\n", students);
return 0;
}
Output
Apples: 10
Temperature: -5
Students: 350
2. float — Floating Point Type
Used to store decimal numbers (numbers with a fractional part). It provides up to 6–7 decimal places of precision.
| Type | Size | Precision |
|---|---|---|
| float | 4 bytes | Up to 6–7 decimal digits |
#include <stdio.h>
int main()
{
float price = 49.99;
float temperature = 36.6;
printf("Price: %.2f\n", price);
printf("Temperature: %.1f\n", temperature);
return 0;
}
Output
Price: 49.99
Temperature: 36.6
3. double — Double Precision Floating Point
Used when more decimal precision is needed than float can offer. It uses 8 bytes and provides up to 15–16 decimal places.
| Type | Size | Precision |
|---|---|---|
| double | 8 bytes | Up to 15–16 decimal digits |
| long double | 12 or 16 bytes | Up to 18–19 decimal digits |
#include <stdio.h>
int main()
{
double pi = 3.14159265358979;
printf("Pi = %.14f\n", pi);
return 0;
}
Output
Pi = 3.14159265358979
4. char — Character Type
Used to store a single character such as a letter, digit, or symbol. A character is enclosed in single quotes. Internally, C stores characters as their ASCII integer values.
| Type | Size | Range |
|---|---|---|
| char | 1 byte | -128 to 127 (signed) or 0 to 255 (unsigned) |
#include <stdio.h>
int main()
{
char grade = 'A';
char symbol = '#';
printf("Grade: %c\n", grade);
printf("ASCII value of A: %d\n", grade);
printf("Symbol: %c\n", symbol);
return 0;
}
Output
Grade: A
ASCII value of A: 65
Symbol: #
Format Specifiers for Data Types
Format specifiers are used with printf() and scanf() to indicate the type of data being read or printed.
| Data Type | Format Specifier | Example |
|---|---|---|
| int | %d | printf("%d", 10); |
| unsigned int | %u | printf("%u", 300u); |
| float | %f | printf("%f", 3.14); |
| double | %lf | printf("%lf", 3.14159); |
| char | %c | printf("%c", 'A'); |
| string | %s | printf("%s", "Hello"); |
| long int | %ld | printf("%ld", 100000L); |
| short int | %hd | printf("%hd", 50); |
sizeof() Operator
The sizeof() operator returns the size (in bytes) of a data type or variable on the current system.
#include <stdio.h>
int main()
{
printf("Size of int : %zu bytes\n", sizeof(int));
printf("Size of float : %zu bytes\n", sizeof(float));
printf("Size of double : %zu bytes\n", sizeof(double));
printf("Size of char : %zu bytes\n", sizeof(char));
return 0;
}
Output (on most 64-bit systems)
Size of int : 4 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of char : 1 byte
Type Modifiers in C
Type modifiers change the storage capacity or the range of basic data types. The four type modifiers in C are:
- signed — Can store both positive and negative numbers (default for int and char)
- unsigned — Stores only positive numbers, doubles the upper range
- short — Uses less memory (2 bytes for int)
- long — Uses more memory (4 or 8 bytes for int)
#include <stdio.h>
int main()
{
signed int a = -100;
unsigned int b = 300;
short int c = 50;
long int d = 1000000;
printf("signed int : %d\n", a);
printf("unsigned int : %u\n", b);
printf("short int : %hd\n", c);
printf("long int : %ld\n", d);
return 0;
}
Output
signed int : -100
unsigned int : 300
short int : 50
long int : 1000000
Void Data Type
The void data type means "no value" or "empty type". It is commonly used in:
- Functions that return nothing:
void printMessage() { ... } - Functions that take no parameters:
int main(void) { ... } - Void pointers: A general-purpose pointer that can point to any data type
Type Conversion in C
Sometimes a value of one data type needs to be converted to another. This is called type conversion.
Implicit (Automatic) Type Conversion
The compiler automatically converts smaller types to larger types to avoid data loss.
#include <stdio.h>
int main()
{
int num = 5;
float result;
result = num; // int automatically converted to float
printf("Result: %.1f\n", result); // Output: 5.0
return 0;
}
Explicit Type Conversion (Casting)
The programmer manually converts one type to another using a cast operator.
#include <stdio.h>
int main()
{
int a = 7;
int b = 2;
float result;
result = (float)a / b; // casting 'a' to float before division
printf("Result: %.2f\n", result); // Output: 3.50
return 0;
}
Summary of Basic Data Types
| Data Type | Size | Used For | Format Specifier |
|---|---|---|---|
| char | 1 byte | Single character | %c |
| int | 4 bytes | Whole numbers | %d |
| float | 4 bytes | Decimal numbers (low precision) | %f |
| double | 8 bytes | Decimal numbers (high precision) | %lf |
| void | — | No value / generic purpose | — |
Summary
Data types are the backbone of any C program. Choosing the correct data type ensures that the program uses memory efficiently and handles data correctly. The four primary data types — int, float, double, and char — cover most everyday programming needs. Type modifiers like unsigned, long, and short provide additional flexibility. Understanding data types thoroughly is essential before moving on to variables, operators, and more complex C concepts.
