A strange phenomenon using casting in C -
the next code:
int main() { int small_num = 0x12345678; int largest_num = 0xffffffff; printf("small: without casting short: 0x%.8x, casting short: 0x%.8x\n", small_num>>16, (short)(small_num>>16)); printf("large: without casting short: 0x%.8x, casting short: 0x%.8x\n", largest_num>>16, (short)(largest_num>>16)); homecoming 0; } gives me output (using codepad):
small: without casting short: 0x00001234, casting short: 0x00001234 large: without casting short: 0xffffffff, casting short: 0xffffffff that's seems extremely strange. have thought why happens way?
when casting (short) in printf call, compiler cast short int, parameter passed printf. therefore, 1234 mapped 1234, , ffff (which -1) mapped ffffffff. note negative integers expanded short long adding "on bits" on left.
c
No comments:
Post a Comment