In C programming, understanding memory and how variables are stored is a fundamental concept. Every variable in C occupies a specific memory location, and knowing the address of a variable can be extremely useful when working with pointers, arrays, and dynamic memory. Printing the address of a variable is one of the first steps for beginners to visualize memory allocation and understand how C handles data internally.

with hands-on learning.
get the skills and confidence to land your next move.
This concept is widely used in debugging, memory management, and when passing variables by reference to functions. By learning how to print the address of a variable, beginners can strengthen their understanding of pointers, a powerful feature of C that allows direct memory access and manipulation. In this article, we’ll explore multiple beginner-friendly programs to print the addresses of variables and explain their practical applications.
Program 1: Print Address of Predefined Variables
This program demonstrates how to print the memory address of a predefined variable. It is a simple way for beginners to learn about memory addresses in C.
#include <stdio.h>
int main() {
int num = 10;
char ch = 'A';
float f = 3.14;
printf("Address of num: %p\n", (void*)&num);
printf("Address of ch: %p\n", (void*)&ch);
printf("Address of f: %p\n", (void*)&f);
return 0;
}
In this program, we use the &
operator to get the address of a variable. The %p
format specifier is used to print pointer addresses, and casting to (void*)
ensures portability across compilers. Beginners can see how each variable occupies a unique memory location. This foundational concept is essential for understanding pointers and memory management in C.
Program 2: Print Address of User-Defined Variables
To make the program interactive, we can allow the user to input values for variables and then print their memory addresses.
#include <stdio.h>
int main() {
int num;
char ch;
float f;
printf("Enter an integer: \n");
scanf("%d", &num);
printf("Enter a character: \n");
scanf(" %c", &ch);
printf("Enter a float: \n");
scanf("%f", &f);
printf("\nAddress of num: %p\n", (void*)&num);
printf("Address of ch: %p\n", (void*)&ch);
printf("Address of f: %p\n", (void*)&f);
return 0;
}
Here, the user provides input for the variables. Using the &
operator with user-defined variables shows that the memory location is independent of the variable’s value. This program helps beginners understand that variables are stored in memory, and their addresses can be accessed for further operations like pointer assignments or passing by reference.
Program 3: Print Address Using Pointers
Using pointers to store and print addresses is a natural next step in learning about memory in C. This program demonstrates how pointers work with addresses.
#include <stdio.h>
int main() {
int num = 25;
int *ptr = #
printf("Address of num: %p\n", (void*)&num);
printf("Address stored in ptr: %p\n", (void*)ptr);
printf("Value of num through ptr: %d\n", *ptr);
return 0;
}
In this program, we declare a pointer ptr
and assign it the address of num
. Printing the pointer shows the same memory location as num
, and using *ptr
retrieves the value stored at that address. Beginners can see how pointers serve as variables that store addresses and provide access to the values in memory, which is crucial for dynamic memory allocation and efficient programming.
Program 4: Print Address of Array Elements
You can also print the addresses of individual elements in an array. This program demonstrates how arrays are stored in contiguous memory locations.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
for(i = 0; i < 5; i++) {
printf("Address of arr[%d]: %p\n", i, (void*)&arr[i]);
}
return 0;
}
Here, each element of the array has its own memory address, stored consecutively. This program helps beginners understand that arrays are stored in contiguous memory, which is essential for understanding pointer arithmetic, efficient array handling, and memory optimization in C programs.
Frequently Asked Questions (FAQ)
Here are some common questions beginners have about printing the address of variables in C.
Q1. What does the &
operator do?
It returns the memory address of a variable.
Q2. What is a pointer?
A pointer is a variable that stores the memory address of another variable.
Q3. Why use %p
for printing addresses?%p
is the standard format specifier for pointer addresses in C.
Q4. Can the address of a variable change during program execution?
Local variables have fixed addresses during execution, but memory allocation for dynamic variables may change.
Q5. Why cast to (void*)
when printing addresses?
Casting to (void*)
ensures portability and avoids warnings across different compilers.
Conclusion
Printing the address of a variable is a key concept in C programming that introduces beginners to memory management and pointers. By understanding how to access and use memory addresses, you can write more efficient programs, pass variables by reference, and manipulate data directly in memory. Practicing these programs with predefined variables, user input, pointers, and arrays will give you a strong foundation in understanding how C handles memory, preparing you for advanced topics like dynamic memory allocation and data structures.
Additional & References
For beginners who want to explore pointers, memory addresses, and C programming further, the following resources are very helpful:
- C Standard Library (stdio.h) – Documentation for input/output functions like
printf()
andscanf()
, essential for handling addresses. - Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
- GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
- Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.