C语言用scanf输入一个%s的字符串 怎么判断其中的内容?

2024-11-08 03:14:44
推荐回答(1个)
回答1:

成绩 char cj[10];
输入字符串 scanf("%s",cj);
字符串转数值 sscanf(cj,"%f",&v);
程序如下:
#include
main()
{
char cj[10];
float v;
printf("input cj, if cj=='q' then exit\n");
while(1){
scanf("%s",cj);
if (sscanf(cj,"%f",&v)!=1) break;
if (v>=60)printf("cj is %g, pass\n",v);
else printf("cj is %g, not pass\n",v);
}
printf("good bye\n");
return 0;
}
例子:
input cj, if cj=='q' then exit
12.5
cj is 12.5, not pass
46
cj is 46, not pass
60
cj is 60, pass
86.3
cj is 86.3, pass
q
good bye