How long to print? Doesn't that work? % Li

I read the documentation and it says that% li is long, but the listing is returning as -2147024891. What gives?

+4
source share
6 answers

You did not even indicate which number you want to print, but I think you stumbled upon the difference between the signature and the unsigned seal.

Use "% lu" for unsigned long numbers and "% ld" or "% li" for signed long numbers.

MSDN has good documentation on printf specifications. For 64-bit values โ€‹โ€‹(for example, long long , for example) you should use macros in "inttypes.h".

+12
source

You are trying to print HRESULT, the error code for "access denied". It is best formatted in hexadecimal, at least it is easy to recognize the programmer.

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

Now you will immediately recognize the object code 7 (Windows API) and error code 5 (access is denied).

+7
source

Are you printing the unsigned value as signed? With two additions, the value with the most significant set of bits will be printed as negative. Try% u for unsigned

0
source

% ld or% lu depending on what you need.

0
source

Based on 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); } 

Output compiled in 32-bit mode (on MacOS 10.6.2):

 +2147024891 = 0x7ff8fffb -2147024891 = 0x80070005 

Output compiled in 64-bit mode:

  2147024891 = 0x7ff8fffb -2147024891 = 0xffffffff80070005 

Since you are not showing us what you wrote, and why you were expecting something else, it is hard to say what might be wrong. However, although relatively unusual, the conversion " %li " was in C89 as well as C99, so this should be correct. More normal conversion specifiers are " %ld " (synonymous with " %li ") and " %lu " (and " %lo " and " %lx ") for unsigned values.

0
source

Try using% llu, and also to assign a value to a long variable, do not forget that you must add "LL" at the end of the number, for example a = 999999999LL

0
source

Source: https://habr.com/ru/post/1301209/


All Articles