C Memory Address
Every variable in a running program is stored somewhere in memory, and that location has a numeric address. The & (address-of) operator lets you find out where a variable lives.
Understanding memory addresses is the foundation for pointers, one of C's most powerful (and tricky) features.
&variableThe address-of operator
Placing & before a variable name gives you its memory address rather than its value. Addresses are usually printed with the %p format specifier.
Why addresses matter
Functions like scanf need addresses to modify variables outside their own scope, and pointers store addresses so they can indirectly access or modify other variables.
#include <stdio.h>
int main() {
int age = 25;
printf("%p\n", (void*)&age);
return 0;
}0x7ffee3a1b4ac&age gives the memory address where age is stored; exact addresses vary each run.
#include <stdio.h>
int main() {
int a = 5, b = 5;
printf("%d\n", &a == &b);
return 0;
}0Two different variables have two different addresses, even if their values are the same.
Key points
- The & operator returns a variable's memory address.
- Memory addresses are typically printed with %p.
- Each variable has its own unique address.
- Memory addresses are the basis for how pointers work.
