【发布时间】:2016-04-29 07:06:35
【问题描述】:
我试图找出在 C 标准中printf() 打印的字符数的允许限制是多少。我只在一个讨论区找到了answer,上面写着INT_MAX。
例如,我检查了以下内容:
#include <stdio.h>
// INT_MAX 2147483647
int main()
{
int x=3;
int y = printf("%2147483647d \n\n", x); --> Confirms INT_MAX ?
// If I change the above to 2147483648, y in set to -1
printf("y = %d\n\n", y);
return 0;
}
我想问为什么printf() 会被INT_MAX 限制?谁能指出 C 标准推理或源代码参考?
编辑
我找到的最接近的答案是fprintf()。在这个link,在第 568 页,它提到(在未定义的行为下):
The number of characters or wide characters transmitted by a formatted output
function (or written to an array, or that would have been written to an array) is
greater than INT_MAX.
以上内容也能证明printf() 的合理性吗?
【问题讨论】:
-
printf()被INT_MAX限制并不奇怪。如果有疑问,它会受到INT_MAX的限制。 -
但是是什么原因呢?
-
它显然使用
int来存储打印的字符数量。您不能在int中存储大于INT_MAX的值。 -
规范说“十进制整数”。可以合理地假设这意味着
int -
"以上内容是否也可以证明 printf() 的合理性?" 是的,根据 7.21.6.3/2 "printf 函数等效于带有参数的 fprintf标准输出插入 printf 的参数之前。”