C - Address of Operator

Introduction

The address of operator, &, returns the address in memory.

You have been using the address of operator with the scanf() function.

The following program outputs the address of some variables:

Demo

#include<stdio.h>

int main(void)
{
  // Define some integer variables
  long a = 1L;/*from  w  ww .  j a v  a  2 s  .co  m*/
  long b = 2L;
  long c = 3L;

  // Define some floating-point variables
  double d = 4.0;
  double e = 5.0;
  double f = 6.0;

  printf("A variable of type long occupies %u bytes.", sizeof(long));
  printf("\nHere are the addresses of some variables of type long:");
  printf("\nThe address of a is: %p  The address of b is: %p", &a, &b);
  printf("\nThe address of c is: %p", &c);
  printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
  printf("\nHere are the addresses of some variables of type double:");
  printf("\nThe address of d is: %p  The address of e is: %p", &d, &e);
  printf("\nThe address of f is: %p\n", &f);
  return 0;
}

Result

how It Works

you declare three variables of type long and three of type double:

// Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;

// Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;

You output the number of bytes occupied by variables of type long, followed by the addresses of the three variables of that type:

printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p  The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);

%u is used for the value produced by sizeof because it will be an unsigned integer value.

specifier %p is used to output the address of the variables.

%p specifier is for outputting a memory address, and the value is presented in hexadecimal format.