📦 C Data Types – Store Different Kinds of Values
In C, data types define the type of data a variable can hold. Whether you’re counting apples or storing grades with decimal points, C has a data type for that!
🔸 Basic Data Types in C
Here are some commonly used data types in C:
Data Type | Description | Example |
---|---|---|
int |
Stores whole numbers | 10, -5, 1000 |
float |
Stores decimal numbers (single precision) | 3.14, -0.5 |
double |
Stores decimal numbers (double precision) | 3.14159265 |
char |
Stores a single character | ‘A’, ‘z’ |
📝 Example: Declaring Different Data Types
This program shows how to declare and use basic data types in C.
#include <stdio.h> int main() { int age = 25; // Integer variable float height = 5.9; // Float variable double pi = 3.14159265; // Double variable char grade = 'A'; // Character variable printf("Age: %d\n", age); printf("Height: %.1f\n", height); printf("Value of Pi: %.8f\n", pi); printf("Grade: %c\n", grade); return 0; }
📏 Data Type Sizes
Here’s a quick look at how much memory (in bytes) each data type generally uses:
int
→ 4 bytesfloat
→ 4 bytesdouble
→ 8 byteschar
→ 1 byte
Note: Actual sizes can vary slightly depending on your system/compiler.
🎯 Pro Tip
Use %d
for int
, %f
for float
, %lf
for double
, and %c
for char
in printf()
.
🧪 Practice Challenge
Try creating a program that declares all 4 basic data types and prints their values using printf()
.