-sizeof()
Zack Weinberg
zack@codesourcery.com
Thu Nov 18 01:48:00 GMT 2004
Jan Ringoš <tringi@mx-3.cz> writes:
> Hello, I am not a C++ guru, but this thing looks weird.
> I have following two codes:
>
> __int64 a = -4;
> printf ("%016I64x\n", a); // prints fffffffffffffffc
In this case, the 4 has type signed int, so the unary minus operator
changes it to -4 (still with type signed int). Assignment to a then
sign-extends.
> __int64 b = -sizeof(int);
> printf ("%016I64x\n", b); // prints 00000000fffffffc
In this case, the sizeof(int) evaluates to 4 - but with type unsigned
int. The unary minus operator changes that to the *positive* value
0xfffffffc, still with type unsigned int. Assignment to b then
zero-extends.
You would observe the same behavior from
__int64 c = -4u;
zw
More information about the Gcc
mailing list