13

I am using Code::Blocks with GCC 4.4.1 and I seem to be unable to print 64-bit signed integers from my C-code.

This code:

long long longint;

longint = 0x1BCDEFABCDEFCDEF; /* 2003520930423229935 */
printf("Sizeof: %d-bit\n", sizeof(longint) * 8);     /* Correct */
printf("%llx\n", longint);                           /* Incorrect */
printf("%x%x\n", *(((int*)(&longint))+1), longint);  /* Correct */
printf("%lld\n", longint);                           /* Incorrect */ 

Produces output:

Sizeof: 64-bit
cdefcdef
1bcdefabcdefcdef
-839922193

64-bit arithmetic seems to work correctly:

longint -= 0x1000000000000000;
printf("%x%x\n", *(((int*)(&longint))+1), longint);

Gives:

bcdefabcdefcdef

Am I missing something?

Fenikso
  • 9,251
  • 5
  • 44
  • 72
  • 1
    Possible duplicate of [this question](http://stackoverflow.com/questions/2844/how-do-you-printf-an-unsigned-long-long-int). – npclaudiu Apr 19 '12 at 06:49
  • How is the second printf incorrect? Ah, I see in the example now. Never mind. The example prints out the right value for me on Linux. –  Apr 19 '12 at 06:50
  • @npclaudiu - Thanks. I got it from there! – Fenikso Apr 19 '12 at 06:55
  • `%lld` and `%llx` are correct format directives for printing a value of type `long long` (in C99). If it doesn't work, there's something broken in the installation, e.g., mismatched or broken libraries. – torek Apr 19 '12 at 06:56
  • For anyone still thinking of using GCC 4.4.1 - that compiler is now over a decade old, and was 2.5 years old at the time the question was asked :) – Kuba hasn't forgotten Monica Mar 12 '20 at 14:13
  • @ReinstateMonica I worked in aerospace back then. It is not uncommon to work with 20 years old software and compilers. – Fenikso Mar 25 '20 at 13:50
  • @Fenikso That's perfectly fine then - I'd rather it be this way, in fact :) – Kuba hasn't forgotten Monica Mar 27 '20 at 15:21

5 Answers5

11

See if %I64d helps you. %lld is fine for long long int but things get really different sometimes on Windows IDEs

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
11

To (in C99 and up) portably print 64 bit integers, you should #include <inttypes.h> and use the C99 macros PRIx64 and PRId64. That would make your code;

printf("Sizeof: %d-bit\n", sizeof(longint) * 8);
printf("%" PRIx64 "\n", longint);
printf("%" PRId64 "\n", longint);

Edit: See this question for more examples.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 2
    Those are technically formats for `int64_t`, in case that happens to differ from `long long`. Of course there's some chance that they actually *work* on existing broken/not-quite-C99-still-yet systems. :-) – torek Apr 19 '12 at 06:58
6

This is OS dependent. If you're doing this on just about any GCC that uses GLIBC, then %llx works.

However if you are using mingw compiler, then this uses Microsoft libraries, and you need to look into their documentation.

This changes your program to:

longint = 0x1BCDEFABCDEFCDEFLL; /* 2003520930423229935 */
printf("Sizeof: %d-bit\n", sizeof(longint) * 8);     /* Correct */
printf("%I64x\n", longint);                           /* Incorrect */
printf("%x%x\n", *(((int*)(&longint))+1), longint);  /* Correct */
printf("%I64d\n", longint);
dbrank0
  • 9,026
  • 2
  • 37
  • 55
1

longint = 0x1BCDEFABCDEFCDEF; /* 2003520930423229935 */ you can print as-

printf("%llx", longint);

Amit Rai
  • 11
  • 1
0

Finally got it:

longint = 0x1BCDEFABCDEFCDEF; /* 2003520930423229935 */
printf("%I64d\n", longint);
printf("%I64x\n", longint);

Prints:

2003520930423229935
1bcdefabcdefcdef

Thanks @npclaudiu!

Fenikso
  • 9,251
  • 5
  • 44
  • 72