在 printf 格式字符串中使用 %zu 的语法是否正确,如维基百科上找到的一些 C 代码所示?

发布于 2024-09-03 04:57:14 字数 515 浏览 8 评论 0原文

我刚刚在维基百科上找到了这段代码。
链接: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只是一片海 2024-09-10 04:57:15

是的,但它仅适用于 C99 兼容的编译器。来自 维基百科

z:对于整数类型,导致 printf 期望 size_t 大小的整数参数。

Yes, but it only works on C99-compliant compilers. From wikipedia:

z: For integer types, causes printf to expect a size_t sized integer argument.

素年丶 2024-09-10 04:57:15

您是否告诉您的编译器您希望它用 C99 大脑进行思考?可能有一个开关可以做到这一点。例如,对于 gcc,-std=c99

如果您的编译器不支持它,但您知道其他编译器会支持它,您可以执行 PRId64 样式的解决方案(免责声明 - PSEUDO CODE AHEAD ..):

#ifdef __SOME_KNOWN_C99_COMPILER
#define PORTUNSIGNED "zu"
#else
#define PORTUNSIGNED "u"
#endif

printf("%-11" PORTUNSIGNED " ways to skin a cat\n");

最好获得具有功能支持的编译器然而对于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 ..):

#ifdef __SOME_KNOWN_C99_COMPILER
#define PORTUNSIGNED "zu"
#else
#define PORTUNSIGNED "u"
#endif

printf("%-11" PORTUNSIGNED " ways to skin a cat\n");

Its probably better to get a compiler that has functional support for c99, however.

最丧也最甜 2024-09-10 04:57:15

我使用 gcc 4.0 进行了测试。它适用于 -std=c99

I've made a test using gcc 4.0. It works with -std=c99

茶花眉 2024-09-10 04:57:15

感谢 @Tim Post 和 @pcent 的 -std=c99 建议。
对于像我这样的新手,更详细一点:
我正在使用 Windows 和 ming_w32_gcc。
当我使用以下方法编译代码时:

gcc -Werror -Wall q3.c

我收到错误消息“格式中的转换类型字符‘z’未知”
但是当我最后添加 -std=c99 时,它工作得很好:

gcc -Werror -Wall q3.c -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:

gcc -Werror -Wall q3.c

I got the error message "unknown conversion type character 'z' in format"
But when I added -std=c99 on the end it worked fine:

gcc -Werror -Wall q3.c -std=c99
美人骨 2024-09-10 04:57:14

是的,语法是正确的(至少对于 C99 来说)。看起来你的编译器还没有设置来处理它。只需取出 z 就可以了。为了确保正确性,请确保您的 printf 格式说明符与类型的大小相匹配。打开编译器向您发出的所有警告可能会在这方面有所帮助。

您的报价:

应该使用 z 前缀来打印它,因为每个架构的实际大小可能不同

这是指 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 your printf 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:

The z prefix should be used to print it, because the actual size can differ on each architecture

is referring to the fact that size_t (which is the type returned by the sizeof operator) can vary from architecture to architecture. The z 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.

一个人练习一个人 2024-09-10 04:57:14

C99标准中为C添加了z长度修饰符;您的编译器可能不支持 C99

如果您的 C 编译器不支持这一点,您可以将大小视为 unsigned long:

printf("%lu,%lu", (unsigned long)sizeof c, (unsigned long)sizeof(int));

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:

printf("%lu,%lu", (unsigned long)sizeof c, (unsigned long)sizeof(int));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文