C99 standard has integer types with bytes size like int64_t. I am using the following code:
#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?
The C99 way is
Or you could cast!
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/In windows environment, use
in Linux, use
//VC6.0 (386 & better)
Regards.
With C99 the
%j
length modifier can also be used with the printf family of functions to print values of typeint64_t
anduint64_t
:Compiling this code with
gcc -Wall -pedantic -std=c99
produces no warnings, and the program prints the expected output:This is according to
printf(3)
on my Linux system (the man page specifically says thatj
is used to indicate a conversion to anintmax_t
oruintmax_t
; in my stdint.h, bothint64_t
andintmax_t
are typedef'd in exactly the same way, and similarly foruint64_t
). I'm not sure if this is perfectly portable to other systems.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:
For
int64_t
type:for
uint64_t
type: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, likeSCNd64
.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 printingint32_t
, and"%d"
or similar for printingint
.