In this article, you will understand how to print a variable’s address in C Programming.
How to Print a Variable’s Address in C Programming?
We have two methods to print a variable’s address in C Programming.
Method 1: Print a Variable Address Using the “address of” Operator
To print the address of a variable using the “address of operator”, we can follow the below-given steps:
Step 1: First declare a variable of any data type and initialize it with a value. The data type could be int, float, or char.
Step 2: Then use the “address of operator” (&) followed by the variable name to get its address.
Step 3: After that, store the address in a pointer variable of type “pointer to the same data type as the original variable”.
Step 4: Then print the address using the printf() function.
The following is an illustration of a C program that prints a variable address using the “address of operator” (&).
int main()
{
int num1;
float num2;
char c;
printf("Please enter an integer value to print its address\n");
scanf("%d", &num1);
printf("Entered number is %d:\n", num1);
printf("Address of %d is: %p\n", num1, &num1);
printf("Please enter a float value to print its address\n");
scanf("%f", &num2);
printf("Entered number is %f:\n", num2);
printf("Address of %f is: %p\n",num2, &num2);
printf("Please enter a character to print its address\n");
scanf("%c", &c);
getchar();
printf("Address of character is: %p\n", &c);
return 0;
}
The above program prompts the user to enter an integer, a float, and a character, then prints their values and memory addresses using the “address of operator” (&) and the printf() function from the standard input/output library. It also uses the scanf() function to read user input and the getchar() function to consume the newline character left in the input buffer by scanf().
Method 2: Print a Variable Address Using Pointer
To print a variable’s address using a pointer, we can follow these steps:
Step 1: Declare a variable of any data type (e.g., int, float, char) and initialize it with a value.
Step 2: Declare a pointer variable of the same data type as the original variable, using the asterisk (*) operator.
Step 3: Assign the address of the original variable to the pointer variable using the “address of operator” (&).
Step 4: Use the pointer variable to print the address of the original variable.
The following is an illustration of a C program that prints a variable address using “pointer”.
int main(void)
{
int num1;
int *ptr_num1 = &num1;
printf("Please enter an integer value to print its address\n");
scanf("%d", &num1);
printf("Entered number is %d:\n", num1);
printf("Address of a: %p\n", ptr_num1);
float num2;
float *ptr_num2 = &num2;
printf("Please enter a float value to print its address\n");
scanf("%f", &num2);
printf("Entered number is %f:\n", num2);
printf("Address of b: %p\n", ptr_num2);
char c;
char *ptr_c = &c;
printf("Please enter a character to print its address\n");
scanf("%c", &c);
getchar();
printf("Address of c: %p\n", ptr_c);
return 0;
}
The above program prompts the user to enter an integer, a float, and a character, then prints their values and memory addresses using pointers. It uses the “address of operator” (&) to assign the memory address of the variables to their corresponding pointer variables, and then prints the address using printf from the standard input/output library (stdio.h).
Output
Conclusion
Printing a variable’s address in C programming can be done utilizing the “address of” operator or pointer variables. The “address of” operator requires the use of the “&” symbol to get the address, while pointer variables require the use of the “*” symbol to declare a pointer variable and “&” to assign the address of the original variable. Both methods allow for the unique address of a variable to be printed and used in C programming.