typedef和printf的格式说明 [英] Typedefs and printf format specifiers

查看:149
本文介绍了typedef和printf的格式说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的typedef的常见用法是使一个变量的类型传达的一个变量的宗旨更好的主意而不必重新定义它背后的存储结构。

A common use of typedefs is to enable the 'type' of a variable to convey a better idea of a variable's purpose without redefining the storage structure behind it.

不过,我也看到的typedef,以此来改变类变量的存储结构一气呵成。

However, I'm also seeing typedefs as a way to change the storage structure for a class of variables in one go.

例如,如果我定义

typedef uint32_t my_offset_t

和有类型的变量 my_offset_t ,从 uint32_t的切换code-基地字符 uint64_t中是改变一行,并重新编译(假设我用的sizeof <一样简单/ code>,而不是硬codeD尺寸)的除的printf / scanf的。

and have variables of the type my_offset_t, switching the code-base from uint32_t to char or uint64_t is as simple as changing one line and recompiling (assuming I've used sizeof rather than hard-coded sizes), except in the case of printf / scanf.

有没有办法根据一些简单的方法类型交换格式说明符,没有包装函数周围的printf / scanf函数,如果-别人的,或者的ifdef?

Is there a way to swap format-specifiers according to the type in some easy way, without wrapper functions around printf/scanf, if-elses, or ifdefs?

谢谢!

对于任何有兴趣的,我修改使用16位偏移量与32位偏移量工作,但希望它能够去到64位的LKM(或别的东西!)如果偏移必须以最小的变化。

推荐答案

这是一个棘手的业务,但&LT; inttypes.h&GT; 从C99,后来显示的方式做到这一点。

It's a tricky business, but the <inttypes.h> from C99 and later shows the way to do it.

有关每个定义的类型,您需要提供自己与一套合适的优先级和SCN宏,小心避免标准化命名空间。

For each of your defined types, you need to provide yourself with an appropriate set of 'PRI' and 'SCN' macros, being careful to avoid standardized namespace.

例如,你可以使用XYZ作为一个具体项目的preFIX,并产生:

For example, you might use XYZ as a project-specific prefix, and produce:

XYZ_PRIx_my_offset_t
XYZ_PRId_my_offset_t
XYZ_PRIX_my_offset_t
XYZ_PRIu_my_offset_t
XYZ_PRIo_my_offset_t

和SCN当量。此外,你在为盐基型的等效宏来定义这些,所以:

and the SCN equivalents. Further, you'd define these in terms of the the equivalent macros for the base type, so:

#define XYZ_PRIx_my_offset_t PRIx32
#define XYZ_PRId_my_offset_t PRId32
#define XYZ_PRIX_my_offset_t PRIX32
#define XYZ_PRIu_my_offset_t PRIu32
#define XYZ_PRIo_my_offset_t PRIo32

在您的code,你建立一个使用 XYZ_PRIx_my_offset_t 宏您的格式字符串:

In your code, you build your format strings using the XYZ_PRIx_my_offset_t macros:

printf("Offset: 0x%.8" XYZ_PRIX_my_offset_t "\n", my_offset_value);

如果您随后需要将改变一切,以64位,您可以编辑的typedef,并适当的宏定义,和code的其余部分保持不变。如果你真细心,你可以得到pretty接近完全不变。

If you subsequently need to change everything to 64-bit, you edit the typedef and the macro definitions appropriately, and the rest of the code remains 'unchanged'. If you're really careful, you can get pretty close to completely unchanged.

请确保您在32位和64位系统编译有很多设置警告。 GCC不会提醒你当前平台上无问题,但他们可能会出现在其他的。 (我只是修正了一些code,它是干净的64位,但不洁净的32位;它现在使用像 XYZ_PRId_int4 宏代替%d个和两个完全编译。)

Make sure you compile on both 32-bit and 64-bit systems with lots of warnings set. GCC will not warn about non-problems on your current platform, but they may show up on the other. (I just fixed some code that was clean on 64-bit but unclean for 32-bit; it now uses a macro like XYZ_PRId_int4 instead of %d and compiles cleanly on both.)

这篇关于typedef和printf的格式说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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