C语言 gets 函数结合 for 循环读取多个字符串
gets 函数是 C 语言中用于读取字符串的函数,可以使用 for 循环结合 gets 函数来读取多个字符串。例如:
#include <stdio.h>
int main() {
char str[100];
int i;
for(i=0; i<5; i++) {
printf("请输入第%d个字符串:", i+1);
gets(str);
printf("您输入的是:%s\n", str);
}
return 0;
}
以上代码中,使用 for 循环读取五个字符串,每次循环都使用 gets 函数读取用户输入的字符串,然后输出该字符串。注意,使用 gets 函数需要注意字符串长度和缓冲区溢出问题,建议使用 fgets 函数来代替 gets 函数。
原文地址: https://www.cveoy.top/t/topic/nuSe 著作权归作者所有。请勿转载和采集!