为了账号安全,请及时绑定邮箱和手机立即绑定

如何用C打印int 64_t类型

如何用C打印int 64_t类型

C
繁星淼淼 2019-07-22 18:44:56
如何用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变量?
查看完整描述

3 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

用C99%j长度修饰符也可以与printf系列函数一起使用,以打印类型的值。int64_tuint64_t:

#include <stdio.h>#include <stdint.h>int main(int argc, char *argv[]){
    int64_t  a = 1LL << 63;
    uint64_t b = 1ULL << 63;

    printf("a=%jd (0x%jx)\n", a, a);
    printf("b=%ju (0x%jx)\n", b, b);

    return 0;}

使用gcc -Wall -pedantic -std=c99不产生警告,程序输出预期的输出:

a=-9223372036854775808 (0x8000000000000000)b=9223372036854775808 (0x8000000000000000)

这是根据printf(3)在我的linux系统上(手册页面明确指出j用于指示转换为intmax_tuintmax_t在我的stdint.h,都是int64_tintmax_t是否使用完全相同的方式,以及类似于uint64_t)。我不确定这是否能完全移植到其他系统上。


查看完整回答
反对 回复 2019-07-22
  • 3 回答
  • 0 关注
  • 3913 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信