Memory is a long street of houses
The computer's memory is like a very long street. Every house has a number, called its address. When you create a variable, C picks a house for it and stores the value inside.
Think of it like this:
int age = 25;is like moving the number 25 into house number 1000. The variable's name isage, its value is 25, and its address is 1000.
The & operator: "the address of"
You have already used & with scanf. &age means "the address of age". You can print an address with %p:
#include <stdio.h>
int main() {
int age = 25;
printf("Value: %d\n", age);
printf("Address: %p\n", (void *) &age);
return 0;
}Output (on your own computer with gcc – the simple C runner in the browser editor can't run this one) (the address will be different on every computer and every run)
Value: 25 Address: 0x7ffdcb9a0ea4
Addresses are usually shown in hexadecimal (with letters a–f). You never need to know the actual number – what matters is that every variable has one.
Why care about addresses?
- They let a function change a variable that belongs to someone else (that's how
scanfworks!). - They let you work with arrays and strings efficiently.
- They let you ask for new memory while the program runs (Module 11).
Remember:
ageis the value.&ageis where that value lives in memory.