Q:

Printing an address of a variable in C

0

Printing an address of a variable in C

To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable:

  1. By using "address of" (&) operator
  2. By using pointer variable

1) By using "address of" (&) operator

When we want to get the address of any variable, we can use “address of operator” (&) operator, it returns the address of the variable.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

#include <stdio.h>

int main(void)
{
	// declare variables 
	int a;
	float b;
	char c;

	printf("Address of a: %p\n", &a);
	printf("Address of b: %p\n", &b);
	printf("Address of c: %p\n", &c);

	return 0;
}

Output

Address of a: 0x7ffd3d518618
Address of b: 0x7ffd3d51861c
Address of c: 0x7ffd3d518617

2) By using pointer variable

A pointer is the type of a variable that contains the address of another variable, by using the pointer; we can also get the address of another variable.

Read more: Pointers in C language

Program:

#include <stdio.h>

int main(void)
{
	// declare variables 
	int a;
	float b;
	char c;
	
	//Declare and Initialize pointers 
	int *ptr_a = &a;
	float *ptr_b = &b;
	char *ptr_c = &c;
	
	//Printing address by using pointers 	
	printf("Address of a: %p\n", ptr_a);
	printf("Address of b: %p\n", ptr_b);
	printf("Address of c: %p\n", ptr_c);

	return 0;
}

Output

Address of a: 0x7ffd3d518618
Address of b: 0x7ffd3d51861c
Address of c: 0x7ffd3d518617

Note: At every run output may change.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
printf() statement within another printf() stateme... >>
<< Input hexadecimal value in C language...