Overwrite variables when linking from files

I have two files:

fir.c

int x = 7;
int y = 5;
int main()
{
    p2();
    printf("%d\n", x);
    return 0;
}

and:

sec.c

double x;
void p2()
{
    x = 6;
}

After linking and compiling these two files, I print xand get 0 as output. Moreover, after printing, yI get 4018000as output. I understand what is xoverwriting y, but why am I getting these values? What exactly does this mean?

+4
source share
1 answer

First of all, I'm sure that you are using "% x" and not "% d in your printf". Secondly, I am sure that it is 40180000, and not 4018000 (additional zero).

Assuming this is true, here where you get this value:

x y ints (4 ). "x" (8 ), .

"6" IEEE ( )

0x01000000 00011000 00000000 00000000 00000000 00000000 00000000 00000000 "x"

HEX = 0x4018000000000000

int

0x01000000 00011000 00000000 00000000

= 0x40180000

"% x" , "40180000".

BTW: , .

+2

Source: https://habr.com/ru/post/1533041/


All Articles