C99标准具有整数类型,字节大小类似于int64_t . 我使用以下代码:
#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)
我应该使用哪种格式来打印my_int变量而不发出警告?
6 回答
对于
int64_t
类型:对于
uint64_t
类型:您也可以使用
PRIx64
以十六进制打印 .cppreference.com has a full listing所有类型的可用宏,包括
intptr_t
(PRIxPTR
) . scanf有单独的宏,如SCNd64
.PRIu16的典型定义是
"hu"
,因此隐式字符串常量级联在编译时发生 .为了使您的代码完全可移植,您必须使用
PRId32
等打印int32_t
和"%d"
或类似打印int
.C99的方式是
或者你可以投!
如果您坚持使用C89实现(特别是Visual Studio),您可以使用开源
<inttypes.h>
(和<stdint.h>
):http://code.google.com/p/msinttypes/使用C99,
%j
长度修改器也可以与printf系列函数一起使用,以打印int64_t
和uint64_t
类型的值:使用
gcc -Wall -pedantic -std=c99
编译此代码不会产生警告,程序将打印预期的输出:这是根据我的Linux系统上的
printf(3)
(手册页明确指出j
用于表示转换为intmax_t
或uintmax_t
;在我的stdint.h中,int64_t
和intmax_t
都以完全相同的方式键入,并且同样适用于uint64_t
) . 我不确定这是否可以完全移植到其他系统 .在Windows环境中,使用
在Linux中,使用
来自嵌入式世界,甚至uclibc并不总是可用,代码就像
uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);
是打印废话或根本不工作 - 我总是使用一个小帮手,允许我正确转储uint64_t十六进制:
//VC6.0(386及更高)
问候 .