printf 转换规范的大小限制

发布于 2024-09-28 10:40:04 字数 194 浏览 1 评论 0原文

printf 转换规范为 % 后跟标志、宽度、精度、长度修饰符和转换说明符。转换规范的大小是否有实际限制?

%s 为 2 个字符长,而 %08.2f 为 6 个字符长。我的问题是,根据 C99 标准,可以创建的格式字符串中的最大单个规范的长度是多少?

printf conversion specifications are % followed by flags, width, precision, length modifier and conversion specifier. Is there practical limit to size of a conversion specification?

I.e. %s is 2 chars long, while %08.2f is 6 chars long. My question is, what is the length of the maximal single specification in a format string that can be created, according to C99 standard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

热血少△年 2024-10-05 10:40:04

没有这样的最大长度转换规范。如果您认为您已经找到了这样的规范,我可以提出一个长一个字符的规范。

例如,考虑字段宽度和精度。标准说它们是十进制整数,但没有指定它们的范围。因此,您可以使用任意大的整数作为字段宽度或精度来编写转换说明符。

There is no such conversion specification of maximum length. If you think you've found such a spec, I can come up with one that is one char longer.

For example, consider field width and precision. The standard says they are decimal integers but does not specify their range. Therefore you can write conversion specifiers with arbitrarily large integers as field width or precision.

风启觞 2024-10-05 10:40:04

如果您指的是文字字符串,则为 4095 个字符

5.2.4.1 翻译限制
...
-- 字符串文字或宽字符串文字中的 4095 个字符(连接后)
...

我已经被 C89 509 个字符的限制所困扰(不适用于 printf/scanf 格式字符串),所以这是 C99 带来的良好变化之一:-)


编辑:glibc 实现(非标准定义

glibc 实现从 read_int 函数获取宽度。
因此,对于这个实现,显然,极限可能是 INT_MAX(我还没有搜索 read_int 函数)。

If you mean a literal string, it's 4095 characters

5.2.4.1 Translation limits
...
-- 4095 characters in a character string literal or wide string literal (after concatenation)
...

I've been bitten by C89 limit of 509 characters (not for printf/scanf format strings), so this is one of the good changes brought on by C99 :-)


Edit: glibc implementation (not Standard definition)

glibc implementation gets the width from a read_int function.
So, for this implementation, apparently, maybe, the limit is INT_MAX (I haven't searched for the read_int function).

桃扇骨 2024-10-05 10:40:04

printf 转换规范是 % 后跟标志、宽度、精度、长度修饰符和转换说明符。转换规范的大小是否有实际限制?

过去我不得不处理几个标准的 printf 实现,我的总体印象是没有施加任何特定的限制。

格式字符串通常是逐字符解析的。 (想想简单的 FSM。)大多数 printf 实现避免缓冲任何内容在内部,甚至对于数字,都使用逐个字符转换为十进制(甚至不是atoi)。

例如,您可以检查 printf 是如何在 FreeBSD 内核(通常从许多其他实现中提取代码)。这无疑是简化的实现(带有一些特定于内核的调整),但它反映了格式字符串的通常处理方式。

注意:刚刚检查了 glibc 的 vfprintf() 实现,它们使用 malloc() 在内部分配了一个缓冲区(如果需要)。所以没有特别的限制。

我的问题是,根据C99标准,可以创建的格式字符串的最大单个规范的长度是多少?

格式说明符是字符串的一部分,据我所知,字符串长度不受标准限制。正如我上面提到的,我从未见过有任何此类限制的实现。

printf conversion specifications are % followed by flags, width, precision, length modifier and conversion specifier. Is there practical limit to size of a conversion specification?

I had to deal in past with several standard printf implementations and my general impression that there is no particular limit imposed.

The format string generally is parsed character by character. (Think simple FSM.) Most printf implementations avoid buffering anything internally and even for numbers use the char by char conversion to decimal (not even atoi).

You can check for example how the printf is implemented inside the FreeBSD kernel (where from many other implementations often lift the code). That is surely simplified implementation (with couple kernel-specific tweaks), yet it reflects how the format string is often handled.

N.B. Just checked glibc's vfprintf() implementation and they allocate internally a buffer (if needed) with malloc(). So neither particular limit there.

My question is, what is the length of the maximal single specification in a format string that can be created, according to C99 standard?

The format specifier is a part of a string and string length to my knowledge isn't limited by the standard. And as I mention above, neither I have ever seen an implementation with any such limit.

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