Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I printf a long? Shouldn't this work? %li

Tags:

c

printf

I read the documentation and it says that a long is %li but the print out comes back as -2147024891. What gives?

like image 789
asmw Avatar asked Feb 13 '10 19:02

asmw


2 Answers

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>.

like image 186
AndiDog Avatar answered Oct 06 '22 00:10

AndiDog


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 and the Google query box.

printf("0x%08lx", hr);

Now you'll instantly recognize the facility code 7 (Windows API) and the error code 5 (access denied).

like image 42
Hans Passant Avatar answered Oct 06 '22 00:10

Hans Passant