用户提问
我想弄清楚scanf("%*s"); 具体含义,
见如下代码,我想在输入后,程序排除不必要的输入,
变量ch正确输入后应该会有个\n在输入队列中。
变量x,y输入后应该有2个\n在输入队列中。
为什么ch正确输入后遇到scanf("%*s"); 还是需要再次输入,而x,y输入后就不需要。请问原因。
不胜感激!
#include
//void chline(char,x,y);
int main(void)
{
char ch;
int x,y;
int n;
printf("enter char\n");
while((ch=getchar()) == '\n')
continue;
printf("ch = %c\n",ch);
scanf("%*s");
printf("enter 2 number\n");
while(scanf("%d%d",&x,&y) != 2)
{
scanf("%*s");
printf("Please enter 2 number:\n");
}
printf("ch = %c, x = %d,y = %d.\n",ch,x,y);
return 0;
}
推荐答案
scanf("%*s")表示跳至下一空白字符,这里主要是中间的*字符起的作用。比如:
int n;
scanf("%*d %*d %d",&n);
printf("%d",n);
return 0;
如果输入的是1 2 3,那么输出的是3,因为前两个已经忽略啦。
辅助答案
用户:这是一个神奇的男人
2017年11月13日
scanf("%*s"); 这个是输入是跳过 一个*
也就是
当你输入一个* 这个星号会跳过而不获取。