开发者

C: Global ,Static variables understanding

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=1开发者_JS百科0 ; // 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.


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.


You are not printing the address of the variable.

To print the address:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜