如何打印在C型的int64_t [英] How to print a int64_t type in C
问题描述
C99标准具有整数类型的字节大小一样的int64_t。我使用下面的code:
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’
我试着用:
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)
我应该使用哪种格式打印my_int变量无需警告?
Which format should I use to print my_int variable without having a warning?
推荐答案
有关的int64_t
键入
#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);
为 uint64_t中
类型:
#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);
您也可以使用 PRIx64
十六进制打印。
you can also use PRIx64
to print in hexadecimal.
CP preference.com有可用的宏的完整列表所有类型,包括使用intptr_t
( PRIxPTR
)。有对scanf的单独宏,如 SCNd64
。
cppreference.com has a full listing of available macros for all types including intptr_t
(PRIxPTR
). There are separate macros for scanf, like SCNd64
.
PRIu16的典型定义是胡
,那么隐含的字符串常量毗连发生在编译时。
A typical definition of PRIu16 would be "hu"
, so implicit string-constant concatenation happens at compile time.
有关您的code是完全可移植,你必须使用 PRId32
等印刷 int32_t
和%D
或印刷类似 INT
。
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
.
这篇关于如何打印在C型的int64_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!