scanf 是否限制了用户可以输入的字节数?

Has scanf a limited bound on the number of bytes that the user can input?

我在某处读到 scanf() 我最多可以输入 15 char 秒。这样对吗?有界吗?

Has scanf() a limited bound on the number of bytes that the user can input?

.

但是,FWIW,我们可以对实际要扫描的输入的长度施加限制。为了那个原因。我们使用最大字段宽度和转换说明符。一个简单的例子是

char arr[15] = {0};
scanf("%14s", arr);

以上格式保证无论用户输入的长度是多少(即输入缓冲区的数据),scanf() 只会读取 14 chars ,因此,即使在 更长的 用户输入的情况下,我们也可以防止缓冲区溢出。

不过,它不会对用户输入施加任何限制。当被询问时,用户仍然可以输入 char 的任意数量(理论上至少 )。

您可以输入任意数量的字符,直到您按下换行符,即输入 。而且,是的,您也可以通过在此处使用 "%ns" 来限制 n 没有要限制的字符