c语言 初学者 输入函数

字符串的输入不能使用scanf("%s",s) , 而应该使用gets(s) 这句话对吗? // 设有定义char s[81]

第1个回答  2020-01-16

C语言中两种方法都可以,但有区别scanf("%s",s)是读取不到空格的。而gets(s)可以读取。
两种方法都是一个一个字符读取的。不同的是scanf是遇到空格或者回车键就会结束,但gets是只有输入回车键才会终止读取字符串!下面是例子:
#include
<stdio.h>
int
main()
{
   
char
x[10];
   
scanf("%s",x);
   
printf("%s",x);
   
return
0;
}
下面是gets;
#include
<stdio.h>
int
main()
{
   
char
x[10];
   
gets(x);
   
printf("%s",x);
   
return
0;
}

相似回答