scanf(" %c",&c);和scanf("%c",&c);在程序中的区别?
以下程序实现:有15个数按从小到大的顺序存放在一个数组里,输入一个数,要求用折半查找法找出该数是数组中第几个元素值。如果该数不在数组中,输出“不在表中”。#include<stdio.h>
void main()
{
int a[15]={1,3,4,5,6,8,12,23,34,44,45,56,57,58,68};
int x,i,flag,place,sign,top,bot,mid;
char c;
flag=1;
while(flag)
{
printf("input number:");
scanf("%d",&x);
sign=0;
top=0;
bot=14;
if(x<a[0]||x>a[14])
place=-1;
while(!sign&&top<=bot)
{
mid=(top+bot)/2;
if(x==a[mid])
{
place=mid;
sign=1;
printf("Has found %d, its position is %d\n",x,place+1);
}
else if(x>a[mid])
top=mid+1;
else
bot=mid-1;
}
if(place==-1||!sign)
printf("can not find %d.\n",x);
printf("Continue ? (Y/N)");
scanf(" %c",&c);
if(c=='N'||c=='n')
flag=0;
}
}
上面这段程序能正确实现程序功能,但是为什么倒数第五行如果我把scanf(" %c",&c);改成scanf("%c",&c);就不能正常输入c的值呢?