Variables
A variable is a named piece of memory that holds a value. In C every variable has a fixed type that you declare up front — it decides how many bytes the variable uses and what kind of values it can hold.
Declaring and assigning
#include <stdio.h> int main(void) { int age; // declaration: reserve space for an int called age age = 36; // assignment: store a value in it int year = 2026; // declaration with an initial value (initialisation) printf("Age %d in %d\n", age, year); age = age + 1; // a variable can change printf("Next year: %d\n", age); return 0; }
Age 36 in 2026 Next year: 37
The general form is type name = value;. The = means "store", not "is equal to".
Common types
| Type | Holds | Example | printf |
|---|---|---|---|
int | Whole numbers | int score = 42; | %d |
double | Numbers with a fractional part | double price = 4.99; | %f |
char | A single character (a small integer) | char grade = 'A'; | %c |
bool (from <stdbool.h>) | true or false | bool done = false; | print as %d |
#include <stdio.h> #include <stdbool.h> int main(void) { int quantity = 3; double unit_price = 2.50; char size = 'L'; bool gift_wrap = true; printf("%d x size %c at %.2f each\n", quantity, size, unit_price); printf("Total: %.2f\n", quantity * unit_price); printf("Gift wrap: %d\n", gift_wrap); return 0; }
3 x size L at 2.50 each Total: 7.50 Gift wrap: 1
%.2f prints a double with two decimal places. Characters use single quotes ('L'); strings use double quotes ("L") — they're different things in C.
Several variables at once
#include <stdio.h> int main(void) { int width = 8, height = 5; int area = width * height; printf("%d x %d = %d\n", width, height, area); return 0; }
8 x 5 = 40
It's legal to declare several variables on one line, but one per line is easier to read and comment.
Always initialise
A local variable that you declare without a value contains whatever bytes happened to be in that memory — an indeterminate value. Reading it is a bug, and the result can change from run to run. The compiler warns you:
#include <stdio.h> int main(void) { int total; total = total + 10; printf("%d\n", total); return 0; }
main.c:5:13: warning: variable 'total' is uninitialized when used here [-Wuninitialized]
The fix is simple: give every variable a value when you declare it, even if it's just 0.
Swapping two values
To swap, you need a temporary third variable — assigning one to the other directly would overwrite a value before you'd saved it:
#include <stdio.h> int main(void) { int a = 1, b = 2; int temp = a; a = b; b = temp; printf("a = %d, b = %d\n", a, b); return 0; }
a = 2, b = 1
Naming
Choose names that say what a value means: seconds_elapsed rather than s, max_users rather than m. Short names like i and n are fine for loop counters and tiny scopes. The rules from lesson 5 apply: letters, digits and underscores, not starting with a digit, not a keyword.
Where to declare
Since C99 you can declare variables anywhere in a block, so declare each one close to where it's first used. Very old code (C89) declared everything at the top of a block, which you'll still see in some projects.
Try it yourself
Store your name's first letter, your age and your height in metres, and print them in one sentence. Then calculate your age in months.
S is 29 years old and 1.74 m tall. That's about 348 months.