gets 和 scanf 和 getchar

一、用gets输入n个字符串:需在循环前,用 getchar(); 吃掉上次输入末尾的回车符。

int n;

char str[1000];

scanf("%d", &n);

getchar();

for(int i = 0; i < n; i++)

{

    gets(str[i]);

}

二、用scanf输入n个字符串:直接循环。

int n;

char str[1000];

scanf("%d", &n);

for(int i = 0; i < n; i++)

{

    scanf(“%s”, &str[i]);

}


三、getch();用来暂停运行,观察程序运行的中间状态。

getchar();用来吃掉上次输入末尾的回车符。


总结:用gets前要考虑getchar();  。用scanf前不用考虑 。

猜你喜欢

转载自blog.csdn.net/tryeverything/article/details/51419140