在 printf 格式字符串中使用 %zu 的语法是否正确,如维基百科上找到的一些 C 代码所示?
我刚刚在维基百科上找到了这段代码。
链接:http://en.wikipedia.org/wiki/Sizeof#Use
代码:
/* the following code illustrates the use of sizeof
* with variables and expressions (no parentheses needed),
* and with type names (parentheses needed)
*/
char c;
printf("%zu,%zu", sizeof c, sizeof(int));
它指出:“应该使用 z 前缀来打印它,因为每个体系结构的实际大小可能有所不同。”
我在我的编译器上尝试了它,但它给出了以下结果:
zu,zu
I just found this code on Wikipedia.
Link: http://en.wikipedia.org/wiki/Sizeof#Use
The code:
/* the following code illustrates the use of sizeof
* with variables and expressions (no parentheses needed),
* and with type names (parentheses needed)
*/
char c;
printf("%zu,%zu", sizeof c, sizeof(int));
It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture."
I tried it on my compiler, but it gives the following result:
zu,zu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,但它仅适用于 C99 兼容的编译器。来自 维基百科:
Yes, but it only works on C99-compliant compilers. From wikipedia:
您是否告诉您的编译器您希望它用 C99 大脑进行思考?可能有一个开关可以做到这一点。例如,对于 gcc,
-std=c99
。如果您的编译器不支持它,但您知道其他编译器会支持它,您可以执行
PRId64
样式的解决方案(免责声明 - PSEUDO CODE AHEAD ..):最好获得具有功能支持的编译器然而对于c99。
Did you tell your compiler that you want it thinking with a C99 brain? There is probably a switch to do that. For instance,
-std=c99
for gcc.If your compiler does not support it, but you know others will, you can do a
PRId64
style work around (disclaimer - PSEUDO CODE AHEAD ..):Its probably better to get a compiler that has functional support for c99, however.
我使用 gcc 4.0 进行了测试。它适用于
-std=c99
I've made a test using gcc 4.0. It works with
-std=c99
感谢 @Tim Post 和 @pcent 的
-std=c99
建议。对于像我这样的新手,更详细一点:
我正在使用 Windows 和 ming_w32_gcc。
当我使用以下方法编译代码时:
我收到错误消息“格式中的转换类型字符‘z’未知”
但是当我最后添加 -std=c99 时,它工作得很好:
Thanks for the
-std=c99
suggestion @Tim Post and @pcent.For novices like me, a bit more detail:
I'm using Windows and ming_w32_gcc.
When I compiled my code using:
I got the error message "unknown conversion type character 'z' in format"
But when I added -std=c99 on the end it worked fine:
是的,语法是正确的(至少对于 C99 来说)。看起来你的编译器还没有设置来处理它。只需取出
z
就可以了。为了确保正确性,请确保您的printf
格式说明符与类型的大小相匹配。打开编译器向您发出的所有警告可能会在这方面有所帮助。您的报价:
这是指
size_t
(这是sizeof
运算符返回的类型) )可能因架构而异。z
旨在使您的代码更加可移植。但是,如果您的编译器不支持它,那就行不通。只需摆弄%u
、%lu
等的组合,直到你得到有意义的输出。Yes that syntax is correct (at least for C99). Looks like your compiler isn't set up to handle it though. Just take out the
z
and you'll probably be fine. To be correct, make sure yourprintf
format specifiers match the size of the types. Turning on all the warnings your compiler will give you probably helps out in that respect.Your quotation:
is referring to the fact that
size_t
(which is the type returned by thesizeof
operator) can vary from architecture to architecture. Thez
is intended to make your code more portable. However, if your compiler doesn't support it, that's not going to work out. Just fiddle with combinations of%u
,%lu
, etc. until you get the output making sense.C99标准中为C添加了
z
长度修饰符;您的编译器可能不支持 C99。如果您的 C 编译器不支持这一点,您可以将大小视为 unsigned long:
The
z
length modifier was added to C in the C99 standard; you might have a compiler that doesn't support C99.If your C compiler doesn't support that, you can probably treat the sizes as unsigned long: