根据beginning linux programing 4th edtion发现C语言scanf()函数还有这样的用法:
char str[80];
scanf("%[^\n]",str);
直到读取到回车键才停止读取,而不是像通常的scanf(“%s”,str);那样读到空白字符就停止读取。换言之上述方法可以读取一个句子而不是通常的只能读取一个单词。
不过缺点也是显而易见的,和gets相仿,这样可能导致溢出漏洞。正如,历史上对scanf系列函数的评价并不高。
#include
int main(void)
{
char str[80];
puts("Please enter some word, end with the key ENTER");
scanf("%[^\n]", str);
printf("What you input is %s", str);
return 0;
}