403

C99 standard has integer types with bytes size like int64_t. I am using Windows's %I64d format currently (or unsigned %I64u), like:

#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);

and I get this compiler warning:

warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’

I tried with:

printf("This is my_int: %lld\n", my_int); // long long decimal

But I get the same warning. I am using this compiler:

~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)

Which format should I use to print my_int variable without having a warning?

1

7 Answers 7

571

For int64_t type:

#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);

for uint64_t type:

#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);

you can also use PRIx64 to print in hexadecimal.

cppreference.com has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64.


A typical definition of PRIu16 would be "hu", so implicit string-constant concatenation happens at compile time.

For your code to be fully portable, you must use PRId32 and so on for printing int32_t, and "%d" or similar for printing int.

13
  • 20
    Complete list of formatting macro constants: en.cppreference.com/w/cpp/types/integer May 14, 2013 at 10:45
  • 14
    And, if using C++ on Linux, be sure to #define __STDC_FORMAT_MACROS before including inttypes.h.
    – csl
    Nov 28, 2014 at 8:50
  • 21
    PRId64 is a macro which internally translates to "lld". So, it is as good as writing printf("%lld\n", t); See description : qnx.com/developers/docs/6.5.0/…
    – Gaurav
    Aug 14, 2015 at 11:32
  • 43
    @Gaurav, that's true on some platforms, but not on all. On my amd64 Linux machine, for instance, PRId64 is defined as ld. Portability is the reason for the macro.
    – Ethan T
    Jun 28, 2016 at 15:06
  • 24
    I think with some extra effort they could have made it even more obnoxious
    – Pavel P
    Oct 27, 2017 at 23:35
79

The C99 way is

#include <inttypes.h>
int64_t my_int = 999999999999999999;
printf("%" PRId64 "\n", my_int);

Or you could cast!

printf("%ld", (long)my_int);
printf("%lld", (long long)my_int); /* C89 didn't define `long long` */
printf("%f", (double)my_int);

If you're stuck with a C89 implementation (notably Visual Studio) you can perhaps use an open source <inttypes.h> (and <stdint.h>): http://code.google.com/p/msinttypes/

2
  • When using msinttypes from the code.google.com link, I need to define __STDC_FORMAT_MACROS. See stackoverflow.com/questions/8132399/how-to-printf-uint64-t.
    – ariscris
    May 28, 2014 at 16:49
  • 1
    Thanks for the heads up, @ariscris. It appears that macro is only required for C++ though. The definitions in the code linked to are inside a #if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS)
    – pmg
    May 28, 2014 at 17:08
23

With C99 the %j length modifier can also be used with the printf family of functions to print values of type int64_t and uint64_t:

#include <stdio.h>
#include <stdint.h>

int main(int argc, char *argv[])
{
    int64_t  a = 1LL << 63;
    uint64_t b = 1ULL << 63;

    printf("a=%jd (0x%jx)\n", a, a);
    printf("b=%ju (0x%jx)\n", b, b);

    return 0;
}

Compiling this code with gcc -Wall -pedantic -std=c99 produces no warnings, and the program prints the expected output:

a=-9223372036854775808 (0x8000000000000000)
b=9223372036854775808 (0x8000000000000000)

This is according to printf(3) on my Linux system (the man page specifically says that j is used to indicate a conversion to an intmax_t or uintmax_t; in my stdint.h, both int64_t and intmax_t are typedef'd in exactly the same way, and similarly for uint64_t). I'm not sure if this is perfectly portable to other systems.

11
  • 15
    If %jd prnts an intmax_t, the correct invocation would be printf("a=%jd (0x%jx)", (intmax_t) a, (intmax_t) a). There is no guarantee that int64_t and intmax_t are the same type, and if they aren't, the behavior is undefined. Apr 25, 2013 at 17:56
  • 4
    You can portably use %jd to print int64_t values if you explicitly convert them to intmax_t before passing them to printf: printf("a=%jd\n", (intmax_t)a). This avoids the (IMHO) ugliness of the <inttypes.h> macros. Of course this assumes that your implementation supports %jd, int64_t, and intmax_t, all of which were added by C99. Aug 12, 2013 at 19:03
  • 3
    @KeithThompson 'Ugliness' is putting it far far far far too kindly mate. It's absolutely hideous. It's ghastly. It's nauseating. It's embarrassing is what it is. Or at least they should be embarrassed, the lot that introduced this. I've never seen these macros but do what this answer and the comments - yours included - suggest.
    – Pryftan
    Dec 3, 2019 at 22:59
  • 1
    @Pryftan I don't quite find them quite as ugly as you do -- and I'm not at all sure how they could be defined in a less ugly manner without language changes. Dec 4, 2019 at 2:28
  • 1
    @AndrewHenle I guess that their reasoning was to avoid incurring extra overhead in printf implementations, which introducing greater verity of length modifiers would do. Their solution, as ugly as it is, resolves to one of the already-supported modifiers at compile time. Jan 23, 2022 at 1:48
18

Coming from the embedded world, where even uclibc is not always available, and code like

uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);

is printing you crap or not working at all -- i always use a tiny helper, that allows me to dump properly uint64_t hex:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

char* ullx(uint64_t val)
{
    static char buf[34] = { [0 ... 33] = 0 };
    char* out = &buf[33];
    uint64_t hval = val;
    unsigned int hbase = 16;

    do {
        *out = "0123456789abcdef"[hval % hbase];
        --out;
        hval /= hbase;
    } while(hval);

    *out-- = 'x', *out = '0';

    return out;
}
2
  • 1
    Shouldn't buf be 34+1 in size? The char array doesn't get null terminated atm. Jan 19, 2022 at 12:51
  • 1
    @Bjartskular Is correct that this function doesn't properly nul terminate. A better fix would be to simply change out to initially point at &buf[32], since there are already about twice as many char's as maximally required here. Also, using static variables like this is begging for problems down the line (e.g. - multithreading). It'd be better if buf was passed in as a parameter with caller requirements on how big it must be (and we nul terminate). Jan 25 at 21:58
12

In windows environment, use

%I64d

in Linux, use

%lld
3
  • 21
    %lld is the format for long long int, which is not necessarily the same as int64_t. <stdint.h> has a macro for the correct format for int64_t; see ouah's answer. Aug 12, 2013 at 19:05
  • @KeithThompson However, since long long is at least 64-bit, printf("%lld", (long long)x); ought to work, except perhaps for -0x8000000000000000, which could not be representable as a long long if that type is not using two's complement. Aug 12, 2013 at 20:54
  • @PascalCuoq: Yes, it should work with the cast (and the exception you mention is very unlikely, applying only to a system that supports two's-complement but doesn't use it for long long). Aug 12, 2013 at 21:10
0

Stumbled upon this question when I was looking for a way to display 64 bit number in hex:

Found out that you can use:

0x%016llx - at least works with my compiler (aarch64 GCC 7.3.0)

-8

//VC6.0 (386 & better)

    __int64 my_qw_var = 0x1234567890abcdef;

    __int32 v_dw_h;
    __int32 v_dw_l;

    __asm
        {
            mov eax,[dword ptr my_qw_var + 4]   //dwh
            mov [dword ptr v_dw_h],eax

            mov eax,[dword ptr my_qw_var]   //dwl
            mov [dword ptr v_dw_l],eax

        }
        //Oops 0.8 format
    printf("val = 0x%0.8x%0.8x\n", (__int32)v_dw_h, (__int32)v_dw_l);

Regards.

Not the answer you're looking for? Browse other questions tagged or ask your own question.