English 中文(简体)
类型和印刷格式
原标题:Typedefs and printf format specifiers

通常使用的是,使变数类型能够传递一个更好的设想,即不重新界定其储存结构。

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<>t改为charuint64_t,作为简单易变的一条线和重编(假设使用的I ve) 除印刷/扫描/>外。

是否有办法以某种易懂的方式交换格式-签字方,而不在<条码>f/<条码>/<条码>上设置包装功能?

感谢!

对于任何感兴趣的人,我修改了LKM,用16倍的冲抵来工作,但希望能够达到64倍(或者说别的!),必要时可作少量改动。

最佳回答

它是一个trick生意,但<inttypes.h> from C99 and subsequently显示了这样做的途径。

对于你们的每一种定义类型,你们都需要向采购处提供一套适当的“优先事项”和“SCN”宏观,小心避免标准化的名称空间。

例如,您可使用XYZ作为特定项目预先确定,并产生:

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当量。 此外,你在基数类对应宏观上界定了这些要素,因此:

#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

在您的法典中,你采用了XYZ_PRIx_my_offset_t的形式。 宏观:

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

如果你随后需要把所有东西都变为64个轨道,那么你会适当编辑这些类型和宏观定义,其余部分则保持不变。 如果你真心谨慎,你可以彻底改变。

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.)

问题回答

请看一下我的earlier question on inttypes.h。 您可以看到,系统如何界定格式的光谱仪,可以与分类法(通过<条码>#define)一致使用,使定制的打印机对您的习惯类型进行检索。

另一种解决办法是,将未签字的类型和<代码>uintmax_t 改为t/code>。 例如:

printf("%ju
", (uintmax_t)n);

如果<代码>n是任何未签字的类型,将正确操作。

关于<代码>*scanf()的功能,你必须读到一个临时物体,然后分配。

(假定你开办的图书馆支持这些特点)。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...