奇葩问题:在32位系统里面打印会影响其他值

问题

    CLOGD("%u,%u", sizeof(long unsigned int), sizeof(long long unsigned int));
    long long unsigned int a = 0x0000000100000002;
    long unsigned int b = 0x00000003;
    CLOGD("%llu,%lu", a, b);
    CLOGD("%lu,%lu", a, b);

实际打印值:

4,8
4294967298,3
4,2

问题:为什么printf 不同格式打印一个long long unsigned int成员的值,会影响到后面 long unsigned int值。

结果

可参考 https://zhuanlan.zhihu.com/p/359424502?utm_id=0
意思是,printf(“%d,%d,%d”,a,b,c,d,e),后面参数abcde会以字节流方式拼接起来,然后根据格式%按长度截取打印,
所以格式%数量和参数数量是不一一对应的!

上面原理能解释通上面打印受到影响的问题,可是我实际测试如下

    long long unsigned int llu = 0x0000000100000002;
    long unsigned int lu = 0x00000003;
    unsigned int u = 0x00000004;
    printf("sizeof llu=%d,lu=%d,u=%d\n", sizeof(llu), sizeof(lu), sizeof(u));
    printf("0x%llx,0x%lx,0x%x\n", llu, lu, u);
    printf("0x%x,0x%x,0x%x,0x%x\n", llu, lu, u);
    printf("0x%x,0x%x,0x%x,0x%x,0x%x\n", llu, lu, u);

打印

sizeof llu=8,lu=4,u=4
0x100000002,0x3,0x4
0x0,0x2,0x1,0x3
0x0,0x2,0x1,0x3,0x4

确实是字节流拼接了,可是第一个llu打印不合理,多了个0,还需要分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值