C - Checking a variable's location

Introduction

A variable's type and size are uncovered by declaring that variable as a specific type.

You can use the sizeof keyword to check its size.

The content of a variable can be viewed by reading the variable's value using the C language function, for example, printf().

The variable's location in memory can be retrieved using the & operator and output using the %p placeholder.

Demo

#include <stdio.h> 

int main() //www.j  a  v a2  s.  com
{ 
   char c = 'c'; 
   int i = 123; 
   float f = 98.6; 
   double d = 6.022E23; 

   printf("Address of 'c' %p\n",&c); 
   printf("Address of 'i' %p\n",&i); 
   printf("Address of 'f' %p\n",&f); 
   printf("Address of 'd' %p\n",&d); 
   return(0); 
}

Result

When the & operator prefixes a variable, it returns a value representing the variable's address, or its location in memory.

That value is expressed in hexadecimal.

To view that value, the %p conversion character is used.

Related Topic