从printf的结果不一致,长长整型? [英] Inconsistent results from printf with long long int?

查看:196
本文介绍了从printf的结果不一致,长长整型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct DummyStruct{
        unsigned long long std;
        int type;
};

DummyStruct d;
d.std = 100;
d.type = 10;

/// buggy printf, unsigned long long to int conversion is buggy.
printf("%d,%d\n",d.std, d.type);  // OUTPUT: 0,100 
printf("%d,%d\n", d.type, d.std); // OUTPUT: 10,100 
printf("%lld,%d\n",d.std, d.type); // OUTPUT: 100,10

请告诉我为什么无符号长长为int的转换不正确printf中处理。我使用的glibc。

Please tell me why unsigned long long to int conversion is not properly handled in printf. I am using glibc.

是在printf的这个bug?

Is this bug in printf ?

的printf为什么没有做内部类型转换?

推荐答案

它您的使用这就是问题所在。
除非格式字符串指定的类型是完全一样的参数类型,然后事情会无法正常工作。

Its your usage that is the problem. Unless the types specified in the format string are exactly the same as the types in the parameters then things will not work correctly.

这是因为编译器推动参数,为入堆栈。结果
没有类型检查或转换。

This is because the compiler pushes the parameters as-is onto the stack.
There is not type checking or conversion.

在运行时的code是拉动堆栈的价值观和推进基于格式字符串值的下一个对象。如果格式字符串错误的,那么预支的数额不正确,你将得到有趣的结果。

At run-time the code is pulling the values of the stack and advancing to the next object based on the value in the format string. If the format string is wrong then the amount advanced is incorrect and you will get funny results.

这篇关于从printf的结果不一致,长长整型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆