ObjectiveC 与 iOS 中对 %d 的不同处理
这里是第一个问题。查看常见问题解答但没有找到答案。
我使用 Kochan 书学习了 Obj C,他将 %i 分类为整数,%f 分类为浮点数,%d 分类为双精度。
使用iOS,似乎它是:%i和%d代表整数,%f代表浮点数
有人可以确认并解释其基本原理吗?我已经浏览过Apple的字符串编程指南。
先感谢您。
1st question here. Reviewed FAQ but did not find answer.
I learned Obj C using Kochan book and he classifies %i for integers, %f for float, %d for double.
Using iOS, it seems that it is rather: %i and %d for integer and %f for float
Can someone confirm and maybe explain the rationale. I already looked through the string programming guide of Apple.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遵循标准 C/C++ 语法。因此 %d 和 %i 都是“有符号整数”,%f 是“十进制浮点数”,将显示浮点数或双精度数。
在不确定为什么 d 和 i 重复的情况下,%f 会浮动和加倍的原因是通过省略号传递的内容受 C 提升规则的约束。如果传递任何小于 int 的整数类型,它将提升为全尺寸 int 进行传递。同样,如果您传递浮点型,它将被提升为双精度型。因此,接收参数的事物只需要考虑整数与浮点数,而不是存储大小。
The standard C/C++ syntax is followed. So %d and %i are both 'signed integer', %f is 'decimal floating point' and will show floats or doubles.
Without being sure why d and i are duplicated, the reason that %f does floats and doubles is that things passed via an ellipsis are subject to the C promotion rules. If you pass any integer type that is smaller than an int, it'll be promoted to a full size int for passing. Similarly, if you pass a float, it'll be promoted to a double. So the thing receiving the arguments just needs to think in terms of integer versus floating point, not about storage size.