Introduction
In C, a variable is a named piece of memory. Before you use one, you must say what type it is, so C knows how many bytes it needs and what the bytes mean.
Python worked out types for you, but C wants to know exactly what you are storing, because it needs to reserve the right number of bytes. That small effort is what makes C programs so fast and so predictable.
How it works
A new variable holds garbage
This is the biggest difference from Python. A local variable that you declare but do not assign does not start at 0. It holds whatever bytes were left in that memory before:
#include <stdio.h>
int main(void) {
int total;
int count = 3;
total = total + count;
printf("%d\n", total);
return 0;
}StepVioz shows such a variable as ?. Real C would quietly print a random number here. StepVioz stops and tells you instead, so you see the bug at once.
Characters are small numbers
A char stores one byte. 'A' is really the number 65 (its ASCII code), so you can do arithmetic with characters:
#include <stdio.h>
int main(void) {
char c = 'A';
printf("%c is %d\n", c, c);
c = c + 2;
printf("now %c\n", c);
char lower = 'a' + (c - 'A');
printf("lower case: %c\n", lower);
return 0;
}Syntax
int count = 0;
double price = 19.5;
char letter = 'A';
long long big = 9000000000LL;
Example
#include <stdio.h>
int main(void) {
int age = 19;
double cgpa = 3.75;
char grade = 'A';
age = age + 1;
printf("Age %d, CGPA %.2f, grade %c\n", age, cgpa, grade);
printf("An int uses %d bytes, a double %d\n", (int)sizeof(int), (int)sizeof(double));
return 0;
}Step through and look at the memory panel on the right. Each variable shows:
- its name and its address (where it lives in memory, in hexadecimal such as
0x7fffefe8); - its value.
| Type | Holds | Size | printf |
|---|---|---|---|
int | whole numbers, about ±2 billion | 4 bytes | %d |
double | numbers with a fraction | 8 bytes | %f |
char | one character, such as 'A' | 1 byte | %c |
long long | very large whole numbers | 8 bytes | %lld |
Line by line
- Three declarations each reserve memory and give it a first value.
age = age + 1;reads the old value and stores the new one in the same box.- The first
printfuses a different format code for each type. (int)sizeof(int)asks how many bytes a type uses, and converts that to an int for%d.
Expected result
Age 20, CGPA 3.75, grade A
An int uses 4 bytes, a double 8
In the real world
Choosing a type is a real engineering decision. A sensor sending millions of readings might use one byte each to save memory, and banking software avoids double for money because decimals are approximate.
Common mistakes
- Using a variable before giving it a value (it holds garbage).
- Putting a double into an int:
int x = 3.9;stores 3, because the fraction is cut off. - Mixing up
'A'(one character, single quotes) and"A"(a string, double quotes).
Best practices
- Initialise each variable where you declare it.
- Use
intfor counting anddoublefor measurements. - Use
long longwhenever a result might pass two billion.
Summary
C variables have fixed types, which set their size and meaning. A local variable starts with garbage until you give it a value, and a char is simply a small number with a matching letter.
Quick check
Key takeaways
- Every variable has a type, a size, an address and a value.
- Give every variable a value before you use it.
- A char is a small number.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.