- What is a Data Type?
A data type tells the compiler what kind of data a variable will store —
like a number, decimal value, or character — and also how much memory to reserve for it.
Each variable in C must be declared with a specific data type.
🧩 Example
#include <stdio.h>
int main() {
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = ‘D’; // Character
// Print variables
printf(“%d\n”, myNum);
printf(“%f\n”, myFloatNum);
printf(“%c\n”, myLetter);
return 0;
}
✅ Output:
5
5.990000
D
- Basic Data Types in C
|
Data Type |
Size (Memory) |
Description |
Example Value |
|
int |
2 or 4 bytes |
Stores whole numbers (no decimals) |
1, 100, -50 |
|
float |
4 bytes |
Stores fractional numbers (up to 6–7 digits after decimal) |
1.99, -5.75 |
|
double |
8 bytes |
Stores large fractional numbers (up to 15 digits precision) |
1.23456789101112 |
|
char |
1 byte |
Stores a single character (letter, number, or symbol) |
‘A’, ‘5’, ‘#’ |
- Why Different Data Types?
Each type is used for a different purpose:
Use int → when you want whole numbers (age, marks, count, etc.)
Use float → when you want decimal values (height, price, temperature)
Use double → when you need very accurate decimal values
Use char → when you want a single character or symbol
- Basic Format Specifiers
Each data type has a specific format specifier used inside printf()
(to tell C how to print the value).
|
Format Specifier |
Data Type |
Example |
Output |
|
%d or %i |
int |
printf(“%d”, 10); |
10 |
|
%f |
float |
printf(“%f”, 3.5); |
3.500000 |
|
%lf |
double |
printf(“%lf”, 3.14159); |
3.141590 |
|
%c |
char |
printf(“%c”, ‘A’); |
A |
|
%s |
string |
printf(“%s”, “Hello”); |
Hello |
⚠️ Important Note
It’s very important to use the correct format specifier for each data type.
If you mix them (for example, using %d for a float), the program may show wrong results or even crash.
🧩 Quick Example — Using All Data Types
#include <stdio.h>
int main() {
int age = 21;
float height = 5.8;
double salary = 75000.50;
char grade = ‘A’;
printf(“Age: %d\n”, age);
printf(“Height: %f\n”, height);
printf(“Salary: %lf\n”, salary);
printf(“Grade: %c”, grade);
return 0;
}
✅ Output:
Age: 21
Height: 5.800000
Salary: 75000.500000
Grade: A
🧩 Quick Summary Table
|
Purpose |
Data Type |
Format Specifier |
Example Value |
|
Whole numbers |
int |
%d |
25 |
|
Decimal numbers |
float |
%f |
3.14 |
|
High precision decimals |
double |
%lf |
2.718281828 |
|
Single character |
char |
%c |
‘B’ |
|
Text (string) |
char[] |
%s |
“Hello” |