#include <stdio.h>
int main()
{
char C, B;
int x;
printf("What comes after G\n");
scanf("%c", &C);
printf("What comes after O\n");
scanf("%c", &B);
printf("What is your age?\n");
scanf("%d", &x);
printf("You said %c comes after G, %c after T and you're %d years old? Right?", C, B, x);
return 0;
}
问题 是每当您运行代码时,它都会跳过第二个问题“O 之后是什么”,然后问“您的年龄是多少?”
我避免程序跳过第二个问题的唯一方法是在代码中添加一个空格
printf("What comes after O\n");
scanf(" %c", &B);
可以看到"和%c之间的空格
你能给我解释一下吗?
最佳答案
您需要吃掉空白(即新行)- 按照手册页 http://linux.die.net/man/3/scanf
关于c - scanf 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139415/