如何用C打印int 64_t类型

如何用C打印int 64_t类型

C99标准具有字节大小类似int 64_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 -vUsing built-in specs.Target: i686-apple-darwin10Configured 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.1Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)

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


繁星淼淼
浏览 3914回答 3
3回答

慕哥9229398

用C99%j长度修饰符也可以与printf系列函数一起使用,以打印类型的值。int64_t和uint64_t:#include&nbsp;<stdio.h>#include&nbsp;<stdint.h>int&nbsp;main(int&nbsp;argc,&nbsp;char&nbsp;*argv[]){ &nbsp;&nbsp;&nbsp;&nbsp;int64_t&nbsp;&nbsp;a&nbsp;=&nbsp;1LL&nbsp;<<&nbsp;63; &nbsp;&nbsp;&nbsp;&nbsp;uint64_t&nbsp;b&nbsp;=&nbsp;1ULL&nbsp;<<&nbsp;63; &nbsp;&nbsp;&nbsp;&nbsp;printf("a=%jd&nbsp;(0x%jx)\n",&nbsp;a,&nbsp;a); &nbsp;&nbsp;&nbsp;&nbsp;printf("b=%ju&nbsp;(0x%jx)\n",&nbsp;b,&nbsp;b); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;}使用gcc -Wall -pedantic -std=c99不产生警告,程序输出预期的输出:a=-9223372036854775808&nbsp;(0x8000000000000000)b=9223372036854775808&nbsp;(0x8000000000000000)这是根据printf(3)在我的linux系统上(手册页面明确指出j用于指示转换为intmax_t或uintmax_t在我的stdint.h,都是int64_t和intmax_t是否使用完全相同的方式,以及类似于uint64_t)。我不确定这是否能完全移植到其他系统上。
打开App,查看更多内容
随时随地看视频慕课网APP