In C programming, data types define the type of data a variable can store. They specify how much memory will be allocated and what operations are permissible. Choosing the right data type is crucial for efficient memory usage and program reliability.
C provides several primary data types. Let’s explore them:
Data Type |
Keyword |
Size (Typical) |
Range (Approximate) |
Integer |
int |
2 or 4 bytes |
-32,768 to 32,767 / -2B to 2B |
Floating Point |
float |
4 bytes |
3.4E-38 to 3.4E+38 |
Double Float |
double |
8 bytes |
1.7E-308 to 1.7E+308 |
Character |
char |
1 byte |
-128 to 127 or 0 to 255 |
Void |
void |
No value |
Represents empty or null |
🧮 1️⃣ int (Integer Type)
Stores whole numbers (positive/negative).
Size: Usually 4 bytes (depends on system architecture).
Example
#include <stdio.h>
int main() {
int age = 25;
printf("Age: %d", age);
return 0;
}
🌊 2️⃣ float (Floating-Point Type)
Stores decimal numbers with single precision.
Example
#include <stdio.h>
int main() {
float price = 99.99;
printf("Price: %.2f", price);
return 0;
}
🔬 3️⃣ double (Double-Precision Float)
Higher precision for floating-point numbers.
Example
#include <stdio.h>
int main() {
double pi = 3.1415926535;
printf("PI: %.10lf", pi);
return 0;
}
🎯 4️⃣ char (Character Type)
Stores single characters.
Use single quotes e.g. 'A'
.
Example
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade: %c", grade);
return 0;
}
🚫 5️⃣ void (Void Type)
Indicates no value or no return type.
Used in functions that don’t return any value.
Example
#include <stdio.h>
void greet() {
printf("Hello, World!");
}
int main() {
greet();
return 0;
}
🗂️ Data Type Modifiers
Modifiers adjust the size or sign of basic data types:
- short
- long
- signed
- unsigned
Example
unsigned int population = 1000000;
long int distance = 123456789L;
📌 Key Takeaways
- C has five basic data types:
int
, float
, double
, char
, and void
.
- Always choose data types wisely to ensure optimal memory usage.
- Type modifiers (like
unsigned
, long
) allow more flexibility.