#include main (){ unsigned int a = 0xf0ad; char *c = (char *) &a; printf("value @c: %x",*c); }
-
value @c: ffffffad
-
value @c: 0xf0ad
-
compilation error
-
syntax error
On a little-endian system (x86/x64), the 32-bit value 0xf0ad is stored in memory as bytes 0xad 0xf0 0x00 0x00 (least significant byte first). When cast to char*, the pointer addresses the first byte (0xad). When this char value (0xad = -1 as signed char) is promoted to int for printf with %x, sign extension extends it to 0xffffffffad, which printf displays as ffffffad. This demonstrates endianness and sign extension behavior.
On a little-endian machine the first byte in memory of a (0xf0ad) is the low-order byte, 0xad. Casting the address to char * and dereferencing reads just that single byte. Since plain char is signed on most platforms, 0xad (10101101) has its high bit set, so it is sign-extended to a full int when passed to printf's %x, producing ffffffad rather than ad.