要指出的是,我是C语言的初学者,只是在C程序中遇到了输入字符串的怪异方法:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char ch,string[100],sent[100];
scanf("%c",&ch);
scanf("%s",&string);
scanf("%[^\n]%*c",&sent);
printf("%c\n",ch);
printf("%s\n",string);
printf("%s",sent);
return 0;
}
这是错误:最后一行(句子)不打印,不知道我错了,但是在研究中我找到了以下代码:
scanf(" %[^\n]%*c",&sent); //not theres a space before %[^\n]%*c; and then it worked (wtf)
您能通过在其中添加一个空格来解释为什么它起作用吗?