使用固定大小类型的 scanf/printf(和系列)的正确方法? [英] Correct way to use scanf / printf (and family) with fixed size types?

查看:56
本文介绍了使用固定大小类型的 scanf/printf(和系列)的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这个SO问题,我开始想- 使用具有固定大小类型的 scanf/printf(和系列)的正确方法是什么?

Reading this SO question, I started wondering - what is the correct way to use scanf/printf (and family) with fixed size types?

例如,如果我有 short intint,我会使用 %hd%d> 分别 - 很好.

For example, if I have short int or int, I'd use %hd and %d respectively - fine.

但是如果我有 int16_t 呢?short int 可能与 int16_t 不同,它是平台相关的.其他任何固定大小(整数)类型都一样吗?

But what if I have int16_t? short int may be different from int16_t, it's platform dependent. The same for any other fixed-size (integral) types?

注意:看起来我收到了一些反对票,因为我没有尝试用谷歌搜索",它看起来我需要解释一下:我在这里没有看到类似的问题,这就是我发布它的原因.SO 中的大多数问题都可以使用 Google 来回答,而不是在这里提问.这将使 StackOverflow 不再是现在的位置.

NOTE: As it looks like I received some down-votes, because I "didn't try to google this", it looks like I need to explain: I didn't see similar question here, that's why I posted it. Most of the questions in SO could have been answered using Google, instead of asking here. That would make StackOverflow not the place, that it actually is now.

不,我这样做不是为了声誉——我今天已经达到了每日声誉上限(在发布这个问题之前有 24 票赞成).

And NO, I didn't do this for reputation - I already hit the daily reputation cap today (having 24 up votes before posting this question).

我的观点是 - 我认为这不值得投反对票.

My point is - I don't think this deserves down-votes.

推荐答案

正确的做法是使用inttypes.h 定义了 printf 系列和 scanf 系列的标准宏,例如

The correct way is to use inttypes.h which defines standard macros for printf family and the scanf family, e.g.

printf ("%" PRId16, short_int);
scanf ("%" SCNd16, &short_int);

这篇关于使用固定大小类型的 scanf/printf(和系列)的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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