I read the documentation and it says that a long is %li but the print out comes back as -2147024891. What gives?
You didn't even provide which number you wanted to print, but I guess you've stumbled over the difference between signed and unsigned printing.
Use "%lu" for unsigned long
numbers, and "%ld" or "%li" for signed long
numbers.
The MSDN has good documentation on printf specifiers. For 64-bit values (like long long
, for example), you should use the macros in "inttypes.h".
Are you printing a unsigned value as signed? With two's complement, a value with the most significant bit set will be printed as a negative. Try %u for unsigned
You are trying to print an HRESULT, the error code for "access denied". That is best formatted in hex, at least to be easily recognizable to a programmer.
printf("0x%08lx", hr);
Now you'll instantly recognize the facility code 7 (Windows API) and the error code 5 (access denied).
Given the code:
#include <stdio.h>
int main(void)
{
long l1 = +2147024891;
long l2 = -2147024891;
printf("%+11li = 0x%lx\n", l1, l1);
printf("%+11li = 0x%lx\n", l2, l2);
return(0);
}
The output compiled in 32-bit mode (on MacOS 10.6.2) is:
+2147024891 = 0x7ff8fffb
-2147024891 = 0x80070005
The output compiled in 64-bit mode is:
2147024891 = 0x7ff8fffb
-2147024891 = 0xffffffff80070005
Since you don't show us what you wrote and why you expected something different, it is hard to say what could be wrong. However, although relatively unusual, the '%li
' conversion was in C89 as well as C99, so that should be correct. The more normal conversion specifiers are '%ld
' (a synonym of '%li
') and '%lu
' (and '%lo
' and '%lx
') for unsigned values.
Try %llu and also for assigning a value to a long variable dont forget you must add "LL" in the end of the number, for instance a=999999999LL