我的一个朋友正在尝试学习c(她自己和一本书),有时她会寻求帮助。我不明白为什么我不能在c中得到三个输入
她只是给了我一些我无法回答的东西;我很惭愧,但我在大学学习了C,然后转移到了PHP。我很困难,所以我想知道为什么我们不能得到三个输入。这里的部分代码:
#include <stdio.h>
int main()
{
int num1;
int num2;
char x;
printf("Enter a number:\n");
scanf("%d\n",&num1);
printf("Enter another number:\n");
scanf("%d\n",&num2);
printf("Choose an operation sign:\n");
scanf("%c\n",&x);
...
喜欢这个询问的第一个输入两次,就像这样:
Enter a number:
1
2
Enter another number:
3
Choose an operation sign:
-
如果我删除它跳过最后一个scanf
的\n
。
你能帮我理解为什么吗?
注意,上面的例子输入,你会得到'num1 == 1','num2 == 2','x =='3''。 –
如果您删除\ n,程序不会跳过上一次scanf。相反,\ scanf中仍留在缓冲区中的\ n存储在变量x中。 – Farhan