How to know the address of global variables
Code:
#include<stdio.h>
int a = 1;
int b = 2;
int main()
{
int a = 10;
int b = 20;
printf("a = %d \n", a);
printf("b = %d \n", b);
printf("Location of a = %p \n", &a);
printf("Location of b = %p \n", &b);
return 0;
}
a = 10
b = 20
Location of a = 0061FF2C
Location of b = 0061FF28
Program print the address of local variables
Program show the memory location of local as well as global variable
Code:
#include<stdio.h>
int x = 1;
int y = 2;
int main()
{
int a = 10;
int b = 20;
printf("x = %d \n", x);
printf("y = %d \n", y);
printf("a = %d \n", a);
printf("b = %d \n", b);
printf("a = %p \n", &a);
printf("b = %p \n", &b);
printf("x = %p \n", &x);
printf("p = %p \n", &y);
return 0;
}
x = 1
y = 2
a = 10
b = 20
a = 0061FF2C
b = 0061FF28
x = 00404004
y = 00404008
What happen if we declare global and local variable with same name and where does global variable store and how can we get their address
Code:
#include<stdio.h>
int a = 1;
int b = 2;
int main()
{
int a = 10;
int b = 20;
printf("x = %d \n", x);
printf("y = %d \n", y);
printf("a = %d \n", a);
printf("b = %d \n", b);
printf("a = %p \n", &a);
printf("b = %p \n", &b);
return 0;
}