printf 是否需要 %zu 说明符? [英] Is the %zu specifier required for printf?

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

问题描述

我们在嵌入式平台上使用 C89.我试图打印出一个 size_t,但没有成功:

We are using C89 on an embedded platform. I attempted to print out a size_t, but it did not work:

#include <stdio.h>
int main(void) {
    size_t n = 123;
    printf("%zu\n",n);
    return 0;
}

我得到了 zu 而不是 123.
其他说明符正常工作.

Instead of 123, I got zu.
Other specifiers work correctly.

如果 size_t 存在,zu 是否也应该在 printf 中可用?
这是我应该联系我的库供应商的事情,还是允许库实现排除它?

If size_t exists shouldn't zu also be available in printf?
Is this something I should contact my library vendor about, or is a library implementation allowed to exclude it?

推荐答案

如果 size_t 存在,那么 zu 不应该在 printf 中可用吗?

If size_t exists shouldn't zu also be available in printf?

size_t 至少自 C89 以来就存在,但相应的格式说明符 %zu(特别是长度修饰符 z)仅添加到标准中从 C99 开始.

size_t existed at least since C89 but the respective format specifier %zu (specifically the length modifier z) was added to the standard only since C99.

因此,如果您不能使用 C99(或 C11)并且不得不在 C89 中打印 size_t,您只需要回退到其他现有类型,例如:

So, if you can't use C99 (or C11) and had to print size_t in C89, you just have to fallback to other existing types, such as:

printf("%lu\n", (unsigned long)n);

这篇关于printf 是否需要 %zu 说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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