小班语言表演视频:c语言中关于scanf和getchar的问题

来源:百度文库 编辑:高校问答 时间:2024/04/25 13:40:27
大家看这个程序:
#include "stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters:\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
getch();
}
程序中的getchar只用了一次就把用户输入的字符赋给了变量c。
而下面这个程序:
#include "stdio.h"
void main()
{int i,j,flag,m,Y,y,N,n;
char a;
leap1:{printf("Enter a integer:\n");
scanf("%d",&i);
flag=1;
for(j=2;j<=i-1;j++)
if(i%j==0)
{flag=0;
break;}
printf("%d:",i);
if(flag)printf("yes!\n");
else printf("NO!\n");
printf("Please Input Y/N to Try Again or Exit\n");}
scanf("%c",&a);
a=getchar();
if (a=='Y') goto leap1;
if (a=='y') goto leap1;
else printf("Thank you!!");
getch();}
却先用scanf把值赋给变量A,再用getchar()得到A的值。按理说可以直接用getchar把输入值赋给A,可是为什么我试了不行。。
请高手大侠们指教啊。~~~

留名