#include <stdio.h>
#include <math.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[10],b[10];
puts("enter");
gets(a);
puts("enter");
gets(b);
puts("enter");
puts(a);
puts(b);
}
return 0;
}
输出
1
输入
输入
surya(用户输入的字符串)
输入
surya(最后一个puts函数起作用)
如何在C程序中多次使用“ gets”功能?
您应该从不在程序中使用gets()
。不推荐使用。请改用fgets()
。最好在这里解释原因:
Why is the gets function so dangerous that it should not be used?
在a
循环内也b
和while
的定义也没有意义。
这里是更正的程序:
#include <stdio.h>
int main()
{
int t;
char a[10],b[10];
if(scanf("%d",&t) != 1)
{
printf("Error at scanning!");
return 1;
}
while(t--)
{
puts("Enter string A: ");
fgets(a,sizeof a, stdin);
puts("Enter string B: ");
fgets(b,sizeof b, stdin);
printf("\n");
puts(a);
puts(b);
printf("\n\n");
}
return 0;
}