sprintf for unsigned _int64

I don’t know what POSIX has to say about this, but this is nicely handled by core C99:

#include <stdio.h>
#include <inttypes.h>

int main(void) {
    uint64_t dbFileSize = 99;
    uint64_t fileSize = 100;
    char buf[128];
    memset(buf, 0x00, 128);
    sprintf( buf, "\nOD DB File Size = %" PRIu64 " bytes \t"
                  " XML file size = %" PRIu64 " bytes\n"
                  , fileSize, dbFileSize );
    printf( "The string is %s\n", buf );
}

If your compiler isn’t C99 compliant, get a different compiler. (Yes, I’m looking at you, Visual Studio.)

PS: If you are worried about portability, don’t use %lld. That’s for long long, but there are no guarantees that long long actually is the same as _int64 (POSIX) or int64_t (C99).

Edit: Mea culpa – I more or less brainlessly “search & replace”d the _int64 with int64_t without really looking at what I am doing. Thanks for the comments pointing out that it’s uint64_t, not unsigned int64_t. Corrected.

Leave a Comment