我已将字符串的大小设置word为 5。我正在使用 进行输入scanf(),格式说明符为%s。虽然字符串的大小为 5,但如果我输入的字符串大于长度 5,它也会占用整个输入。为什么会发生这种情况?
这是我的 C 程序:
#include <stdio.h>
#include <string.h>
int main() {
char word[5];
printf("Enter word: ");
scanf("%s", word);
printf("Word: %s\n", word);
strlwr(word);
printf("To Lower: %s\n", word);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入的AbcDEfghij长度是 10,但它会接受它并正确打印完整的字符串。我怎样才能防止这种情况发生?我不想接受大于尺寸的输入。
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |