Variables must be declared with a type before use: int age = 25;
Basic types and format specifiers
int: whole numbers → %d
float: decimals (about 6–7 digits) → %f
double: decimals (about 15 digits) → %lf
char: a single character → %c
Strings (char arrays) → %s
%.2f prints 2 decimal places. const creates a constant; sizeof gives a type's size in bytes.
💡 Key Takeaways: C Variables & Data Types
Mastering C Variables & Data Types helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
C Variables & Data Types Code Example
C
#include <stdio.h>
int main() {
int age = 25;
float price = 99.5f;
double pi = 3.14159265;
char grade = 'A';
const int MAX_USERS = 100;
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Pi: %.4lf\n", pi);
printf("Grade: %c\n", grade);
printf("Max users: %d, int size: %zu bytes\n", MAX_USERS, sizeof(int));
return 0;
}
Output
Age: 25
Price: 99.50
Pi: 3.1416
Grade: A
Max users: 100, int size: 4 bytes
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
C Variables & Data Types Practice Exercises
Check what you learned in “C Variables & Data Types”.