How to print a int64_t type in C
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/
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
.