Input 有人能解释一下为什么我们需要在第一个SCANF()之后添加SCANF(“\N”)来查找程序中用户的3个输入吗
我打算编写一个代码,从用户那里获取3个输入:一个字符(名为ch)和两个字符串(名为word和句子)。 这是我在运行程序后所期望的: 输入:Input 有人能解释一下为什么我们需要在第一个SCANF()之后添加SCANF(“\N”)来查找程序中用户的3个输入吗,input,printf,scanf,Input,Printf,Scanf,我打算编写一个代码,从用户那里获取3个输入:一个字符(名为ch)和两个字符串(名为word和句子)。 这是我在运行程序后所期望的: 输入: enter the character :k enter the string :monitor enter the sentence :my code works fine. 输出: character is : k string is : monitor sentence is : my code wo
enter the character :k
enter the string :monitor
enter the sentence :my code works fine.
输出:
character is : k
string is : monitor
sentence is : my code works fine.
工作正常的代码:
#include <stdio.h>// line 1
#include <string.h>// line 2
// line 3
int main (void)// line 4
{// line 5
char ch,word[100],sentence[100];// line 6
printf("enter the character :");// line 7
scanf("%c",&ch);// line 8
printf("enter the string :");// line 9
scanf("\n");// line 10
fgets(word,sizeof(word),stdin);// line 11
printf("enter the sentence :");// line 12
fgets(sentence,sizeof(sentence),stdin);// line 13
printf("character is : %c\n",ch);// line 14
printf("string is : %s",word);// line 15
printf("sentence is : %s",sentence);// line 16
return 0;// line 17
}// line 17
#包括//第1行
#包括//第2行
//第3行
int main(void)//第4行
{//第5行
字符ch,单词[100],句子[100];//第6行
printf(“输入字符:”;//第7行
scanf(“%c”,&ch);//第8行
printf(“输入字符串:”;//第9行
scanf(“\n”);//第10行
fgets(word,sizeof(word),stdin);//第11行
printf(“输入句子:”;//第12行
fgets(句子,sizeof(句子),stdin);//第13行
printf(“字符为:%c\n”,ch);//第14行
printf(“字符串是:%s”,word);//第15行
printf(“句子是:%s”,句子);//第16行
返回0;//第17行
}//第17行
我的问题是:
1:我不明白为什么我需要写scanf(“\n”)(在第10行),以便我的程序提示我进行第二次输入。
2:在第15行中,我没有在printf中写入\n,但我的程序仍然在两行中打印它们(字符串是:…&句子是:…)。为什么会发生这种情况