View Address of Variable in C++

View the address in the memory of a variable in C++ programming by using the ampersand(&) sign before the variable name to print the address in the memory of the variable.

Program Printing Address of Variable

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int var = 5;
  cout<<"Value: "<<var<<endl;
  cout<<"Address: "<<&var; //Notice, the ampersand '&' before var.
getch();
}
Output
Value: 5
Address: 0x0019ff48

Program Printing Address of Variables

Program

#include<iostream.h>
#include<conio.h>
void main()
{
 int  var1;
 char var2[10]; //this is array.
   cout<<"Address of var1 variable: "<<&var1<<endl; //printing 'var1' and
   cout<<"Address of var2 variable: "<<&var2; //'var2' address.
getch();
}
Output
Address of var1 variable: 0x0019ff48
Address of var2 variable: 0x0019ff3c

Program Printing Values using Auto

Program

#include<iostream.h>
#include<conio.h>
void main()
{
auto int i = 1; //assigning value to auto 'i'.
{
auto int i = 2; //assigning value to another auto 'i'.
{
auto int i = 3; //assigning value to another auto 'i'.
cout<<i<<endl; //printing 'i' value'3'.
}
cout<<i<<endl; //printing 'i' value'2'.
}
cout<<i; //printing 'i' value'1'.
getch();
}
Output
3
2
1