Variables in C
A variable is a named memory location that stores data. Think of computer memory as a row of numbered boxes — a variable is a labeled box where you store a value that can change.
Declaring and Using Variables
c
#include <stdio.h>
int main() {
// ── Declaration ──────────────────────────────
int age; // whole number
float price; // decimal number (7 digit precision)
char grade; // single character
double pi; // high-precision decimal (15 digits)
// ── Initialization ───────────────────────────
age = 17;
price = 199.99f;
grade = 'A';
pi = 3.14159265358979;
// ── Declare + Initialize in one line ─────────
int marks = 95;
float weight = 65.5f;
// ── Multiple variables, same type ────────────
int x = 5, y = 10, z = 15;
// ── Print ────────────────────────────────────
printf("Age : %d\n", age);
printf("Price : %.2f\n", price);
printf("Grade : %c\n", grade);
printf("Pi : %lf\n", pi);
printf("x+y+z : %d\n", x+y+z);
return 0;
}▶ Output
Age : 17 Price : 199.99 Grade : A Pi : 3.141593 x+y+z : 30
Variable Types — Quick Reference
| Type | Memory | Use Case | Format |
|---|---|---|---|
| int | 4 bytes | Age, count, marks, roll no | %d |
| float | 4 bytes | Price, weight, temperature | %f |
| double | 8 bytes | Scientific calculations | %lf |
| char | 1 byte | Grade, gender, single letter | %c |
| long int | 8 bytes | Large numbers | %ld |
| unsigned int | 4 bytes | Only positive values | %u |
Variable Naming Rules
- Start with letter or underscore:
age,_count✅ - Can contain letters, digits, underscore:
student_marks2✅ - No spaces: use camelCase
studentMarksor snake_casestudent_marks - Case sensitive:
Ageandageare TWO different variables! - Cannot use keywords:
int,float,for,ifetc.
⚠ Warning: Uninitialized variables in C contain garbage values — random leftover data from previous programs. ALWAYS initialize variables before using them!