MSVC++ handling unsigned long long int
I'm having an issue with the way MSVC handles unsigned long long integers. Here's code to reproduce:
// test.cpp (note extension)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned long long int address = 0x0A0B0C0D0E0F;
printf("Address=%llu\n", address);
printf("%02X:%02X:%02X:%02X:%02X:%02X\n",
((address >> (5 * 8)) & 0xff),
((address >> (4 * 8)) & 0xff),
((address >> (3 * 8)) & 0xff),
((address >> (2 * 8)) & 0xff),
((address >> (1 * 8)) & 0xff),
(address & 0xff));
printf("%02X:", ((address >> (5 * 8)) & 0xff));
printf("%02X:", ((address >> (4 * 8)) & 0xff));
printf("%02X:", ((address >> (3 * 8)) & 0xff));
printf("%02X:", ((address >> (2 * 8)) & 0xff));
printf("%02X:", ((address >> (1 * 8)) & 0xff));
printf("%02X\n", (address & 0xff))开发者_JAVA百科;
exit(0);
}
When I compile this on linux, I get (as expected):
Address=11042563100175
0A:0B:0C:0D:0E:0F
0A:0B:0C:0D:0E:0F
However, when I compile this on MSVC++ 2008 Express I get:
Address=11042563100175
0A:00:0B:00:0C:00
0A:0B:0C:0D:0E:0F
Have I formatted my printf statement incorrectly? Or does MSVC leave an extra byte on the stack after the shift/and operations? Or is the issue with something else?
Note: when compiling with MSVC you need to use the '.cpp' file extension to force C++ mode. I believe this is because MSVC in straight C mode does not include (all of) C99 which includes the '%llu' flag for printf.
Simon.
The %X format specifier expects a 32-bit argument. You are passing 64-bits, throwing off the stack. You can use %llX. The CRT is the same for C and C++ code.
精彩评论