【发布时间】:2021-02-23 12:29:52
【问题描述】:
除非我输入\n,否则以下代码不允许我输入任何超过 4095 个字符的内容,我想知道为什么会这样。
#include <stdio.h>
int main(void)
{
int c;
unsigned long long a = 0;
while ((c = getchar()) != EOF)
++a;
printf("\n%llu\n", a);
return 0;
}
例如:
input: '#' * 4094
output: 4094
input: '#' * 4095
output: 4095
input: '#' * 4096
output: 4095
等等……
但是如果我输入\n,我将能够循环更多的4095个字符等等......
input: ('#' * 4096) + '\n' + '#'
output: 4097
input: ('#' * 99999) + '\n' + ('#' * 99999)
output: 8191
【问题讨论】:
-
不同的交换,但你会明白为什么:How to read over 4k input without new lines on a terminal?。短版:这是您终端的限制;不是你的程序。
标签: c loops while-loop line getchar