The printf
format specifier has to match the type of argument passed. %p
expects a void *
argument, passing u.x
that has the type of int
to it is undefined behavior (also see this question), and results on your implementation and architecture in printing the number in hex representation using upper case characters (as this is what usually %p
uses). Ie. 0x4103AE14 = 1090760212
.
To print the address of a variable use address of operator and because the result of &u.x
type is int*
explicitly cast it to void*
to match %p
requirement:
printf("address of float: %p\n", (void*)&u.y);
printf("The address of u.x is: %p\n", (void*)&u.x);
Which will result in printing the same address.
why does the value of the integer get changed?
Because they share the memory.
Please can you explain this behavior?
Most probably your compiler uses IEEE 745 to represent floating point numbers. The number 8.23
is most probably represented as a single precision floating point number 8.229999542236328125
that is encoded as
0x4103AE14
(compare ex. with this site or this site).
Because the floating point variable and integer variable share the memory in an union
(and stars are in proper positions - on your platform sizeof(int) == sizeof(float)
) the same bit representation of the float
value is interpreted as an int
. So when you access the u.x
variable of that union, the bit representation of a float
number encoded as 0x4103AE14
is interpreted as an int
, which results in the number, well, 0x4103AE14
(because endianess of float and int is same) which is equal to 1090760212
in dec.