C语言——这里为什么在%c前要输入一个空格, scanf(" %c",&c);

#include<stdio.h>
# define N 15
void main()
{
int i,number,top,bott,mid,loca,a[N],flag=1,sign;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while(i<N)
{scanf("%d",&a[i]);
if(a[i]>=a[i-1])
i++;
else
printf("enter this data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%d",a[i]);
printf("\n");
while(flag)
{printf("input number to look for:");
scanf("%d",&number);
sign=0;
top=0;
bott=N-1;
if((number<a[0]) || (number>a[N-1]))
loca=-1;
while((!sign) && (top<=bott))
{mid=(bott+top)/2;
if(number==a[mid])
{loca=mid;
printf("Has found %d,its position is %d\n",number,loca+1);
sign=1;
}
else if(number<a[mid])
bott=mid-1;
else
top=mid+1;

}
if(!sign || loca==-1)
printf("can not find %d.\n",number);
printf("continue or not(Y/N)?");
scanf(" %c",&c);
if(c=='N' || c=='n')
flag=0;
}
}
以后输入字符都要这样吗?

那是因为你之前在
printf("input number to look for:");
scanf("%d",&number);

进行输入时,会输入一个回车符作为结束,这个时候在输入缓存里就把这个回车字符存在里面了。当你要再读入一个字符时,
scanf(" %c",&c);

就会默认先把缓存里的回车符读入(如果不加空格),加空格就可以利用格式化的输入跳过一个字符,而达到你的目的了。
试了下,没有很好的解决办法,似乎加一个空格是最方便的方法。来自:求助得到的回答
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-10-11
这个空格是可有可无的,没有实际意义,是作者认为加上的,可能得含义是为了版面好看,在C程序编制时,常常为了版面整洁,方便阅读和修改而增加一些空格来编排程序代码,这是一种较为良好的编程习惯。本回答被网友采纳
第2个回答  2018-04-24
如果不加空格的话,可以在scanf语句前加一条rewind(stdin);就可以了!
第3个回答  2012-11-28
使输入的光标和前面有一个空格的距离。
相似回答