C99标准有整数类型,字节大小如int64_t.我目前使用Windows的%I64d格式(或无符号%I64u),如:

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

我收到这个编译器警告:

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

但我得到了同样的警告.我正在使用这个编译器:

~/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)

在没有警告的情况下,我应该使用哪种格式打印我的_int变量?

推荐答案

对于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以十六进制打印.

所有类型的cppreference.com has a full listing个可用宏,包括intptr_t(PRIxPTR).scanf有单独的宏,比如SCNd64.


PRIu16的典型定义是"hu",因此隐式字符串常量连接发生在编译时.

为了使代码完全可移植,打印int32_t必须使用PRId32等,打印int必须使用"%d"等.

C++相关问答推荐

Tiva TM4C123GXL的I2C通信

DPDK-DumpCap不捕获端口上的传入数据包

为什么在C中二维字符数组会有这样的行为?

如果我释放其他内容,返回值就会出错

在为hashmap创建加载器时,我的存储桶指向它自己

对重叠字符串使用MemMove

从TCP连接启动UDP(C套接字)

在for循环中指向数组开头之前

在C中访问数组中的特定值

使用TCL C API导航到列表中的元素

在C中包装两个数组?

C将数组传递给函数以修改数组

为什么这个分配做得不好呢?

赋值两侧的后置增量,字符指针

我编写这段代码是为了判断一个数字是质数、阿姆斯特朗还是完全数,但由于某种原因,当我使用大数时,它不会打印出来

传递给函数的 struct 中的数组

gdb - 你能找到持有内部 glibc 锁的线程吗?

C 和 C++ 标准如何告诉您如何处理它们未涵盖的情况?

free后内存泄漏?

C语言程序流程解释