Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a int64_t type in C

Tags:

c

stdint

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?

like image 968
rtacconi Avatar asked Feb 10 '12 09:02

rtacconi


People also ask

How to print int64 in C?

h> int64_t t; printf("%" PRId64 "\n", t); for uint64_t type: #include <inttypes. h> uint64_t t; printf("%" PRIu64 "\n", t);

How to print a uint64_ t in C?

The right way to print uint64_t in printf / snprintf family functions is this (source): #define __STDC_FORMAT_MACROS #include <inttypes. h> uint64_t i; printf("%"PRIu64"\n", i);

How do I print long unsigned int?

To print an unsigned int number, use the %u notation. To print a long value, use the %ld format specifier. You can use the l prefix for x and o, too. So you would use %lx to print a long integer in hexadecimal format and %lo to print in octal format.

How do I printf a double?

We can print the double value using both %f and %lf format specifier because printf treats both float and double are same. So, we can use both %f and %lf to print a double value.


2 Answers

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.

like image 77
ouah Avatar answered Sep 23 '22 17:09

ouah


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/

like image 44
pmg Avatar answered Sep 23 '22 17:09

pmg