C Variables
A variable is a named box in memory that holds a value. You give the box a name, store something in it, and later read or change it. In C, every variable also has a type that decides what kind of value it can hold and how much memory it uses.
Declaring a variable
To create a variable, write the type, then the name, and optionally an initial value:
type name = value;The most common basic types are:
| Type | Holds | Example |
|---|---|---|
int |
Whole numbers | int year = 2024; |
float |
Decimal numbers (about 7 significant digits) | float price = 9.99f; |
double |
Decimal numbers with double precision (about 15 digits) | double pi = 3.14159265; |
char |
A single character | char grade = 'A'; |
Here we create a few variables and print them:
#include <stdio.h>
int main(void) {
int age = 21;
double height = 1.82;
char initial = 'M';
printf("Age: %d\n", age);
printf("Height: %.2f m\n", height);
printf("Initial: %c\n", initial);
return 0;
}Age: 21
Height: 1.82 m
Initial: M
The %d, %.2f and %c parts are format specifiers telling printf how to show each value. The next lesson covers them in detail.
Declaring first, assigning later
You can create a variable without a value and give it one afterwards with the assignment operator =:
#include <stdio.h>
int main(void) {
int score; // declaration
score = 85; // assignment
printf("Score: %d\n", score);
return 0;
}Score: 85
Warning: A local variable that has been declared but not assigned contains garbage — whatever bytes happened to be in that memory. Reading it is undefined behaviour. Always give a variable a value before you use it; the safest habit is to initialise it on the same line you declare it.
Using variables in calculations
Variables can be combined with operators, and the result can be stored in yet another variable:
#include <stdio.h>
int main(void) {
int apples = 12;
int oranges = 7;
int total = apples + oranges;
printf("Apples: %d, oranges: %d\n", apples, oranges);
printf("Total fruit: %d\n", total);
printf("Double the apples: %d\n", apples * 2);
return 0;
}Apples: 12, oranges: 7
Total fruit: 19
Double the apples: 24
Characters are small numbers
A char stores one character in single quotes. Under the hood it is a small integer holding the character's code (in ASCII, 'A' is 65). Printing the same variable with %c and %d shows both views:
#include <stdio.h>
int main(void) {
char letter = 'A';
printf("As a character: %c\n", letter);
printf("As a number: %d\n", letter);
printf("Next letter: %c\n", letter + 1);
return 0;
}As a character: A
As a number: 65
Next letter: B
Where variables live
Variables declared inside a function (like the ones in main above) are local: they exist while the function runs and disappear when it returns. Since C99 you can declare variables anywhere in a block, not only at the top — it is good style to declare them close to where they are first used.
Tip: Choose descriptive names.
int seconds_elapsedtells the reader far more thanint s.
Test yourself
Which line correctly declares an integer variable named count with the value 10?
- count = 10;
- int count = 10;
- int = count 10;
- integer count = 10;
What is stored in int x; before anything is assigned (inside a function)?
- Always 0
- An unpredictable garbage value
- The number 1
- A compile error
Which type is best for storing a single letter such as 'Q'?
- int
- char
- double
- float