tags:

views:

95

answers:

2

Hi All,

In following program . I have one doubt. I have declared one global variable . I am
printing the address of the global variable in the function . It is giving me same address when I am not changing the value of global . If I did any changes in the global variables It is giving me different address why...........? Like that it is happening for static also.

#include<stdio.h> 
int global=10 ; // Global variables

void function();

main()
{
        global=20;
        printf ( " %p \n" , global ) ;
        printf ( " Val: %d\n", global ) ;
        function();
        new();
}

void function()
{
        global=30;
        printf ( " %p \n" , global ) ;
        printf ( " Val: %d\n", global ) ;
}

Thanks.

+2  A: 

You are not printing the address of the variable.

To print the address:

printf("%p\n", &global);
Tuomas Pelkonen
+3  A: 

You are not printing the address of the global, you are printing its value. To print the address:

printf ( " %p \n" , & global ) ;

Note the ampersand, which is the "address-of" operator. The "%p" formatter only controls output format, it doesn't make printf() magically take the address for you.

anon