How to print 64-bit integer in GCC 4.4.1?

Fenikso picture Fenikso · Apr 19, 2012 · Viewed 43.4k times · Source

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?

Answer

Pavan Manjunath picture Pavan Manjunath · Apr 19, 2012

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