-1

I have troubles with printing a big 16 digits hex number in c

unsigned long long j = 0x89ABCDEF12893456;
printf("0x%lx\n", j); //prints: 0x12893456
printf("%#lx", j); //prints: 0x12893456

is there a way to print the whole number?

Asaf
  • 7
  • 1
  • 1
    Try `%#018llx` (`ll` == `long long`) – r3mainer Nov 09 '20 at 23:40
  • 2
    The `l` length modifier is for `long` and `unsigned long`. The `ll` length modifier is for `long long` and `unsigned long long`. You have mismatched arguments, resulting in undefined behavior. For `unsigned long long`, use `%llx`, `%llu`, etc. – Tom Karzes Nov 09 '20 at 23:43
  • 1
    If your compiler didn't tell you about that mistake, turn on warnings so it does (`-Wall -Wextra` for gcc and clang). – Shawn Nov 10 '20 at 04:10
  • Does this answer your question? [How do you format an unsigned long long int using printf?](https://stackoverflow.com/questions/2844/how-do-you-format-an-unsigned-long-long-int-using-printf) – phuclv Nov 10 '20 at 06:52

2 Answers2

2

As per the C standard, C11 7.21.6 Formatted input/output functions /7 which discusses length modifiers:

ll (ell-ell): Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long int or unsigned long long int argument; or that a following n conversion specifier applies to a pointer to a long long int argument.

A single l specifies a long or unsigned long. Hence %llx or %#llx is the format specifier you want, possibly with an output length and zero padding as well, if you want the numbers to line up (such as "0x%016llx").

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

That should do it.

#include <stdio.h>
int main () {

unsigned long long j = 0x89ABCDEF12893456;    
printf("%#llx", j); 
return 0;
}
user10191234
  • 531
  • 1
  • 4
  • 24