作者a27839579 ()
看板C_and_CPP
标题[问题] scanf函数的回传值
时间Mon May 6 16:42:23 2019
以下是C scanf函式的原型
函式原型:int scanf ( const char * format, ... );
引数说明:%[*][width][modifiers]type
%[*][宽度][长度修饰]资料型态
传回值:成功配对之引数数目,失败时将传回 EOF
请问下面这两支程式作对比
两支程式执行後在命令提示字元下输入相同格式
ss(整数) (整数)
第一支程式scanf函式回传为TURE
可是第二支程式的第二个scanf函式回传值却是FALSE
是甚麽原因?
是因为scanf格式输入错误?
如果是编译怎麽会成功呢??
感谢!!!
(1).
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int b,c;
printf("请输入整数:");
c=scanf("ss%d",&b);
printf("\n\nscanf的回传值为%d\n\n",c);
printf("\n\n你的输入为%d\n\n",b);
system("pause");
return 0;
}
(2).
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a,b,c;
printf("请输入整数:");
scanf("%d",&a);
printf("\n\n你的输入为%d\n\n",a);
printf("请输入整数:");
c=scanf("ss%d",&b);
printf("\n\n第二个scanf的回传值为%d",c);
printf("\n\n你的输入为%d\n\n",b);
system("pause");
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 223.137.231.238
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1557132145.A.440.html
1F:→ Lipraxde: 你的顺序是不是反了? 05/06 18:14
2F:→ firejox: 我想是换行问题? 05/06 20:55
3F:推 idiont: %d读完後 \n还留在buffer中 05/07 02:54
※ 编辑: a27839579 (223.137.231.238), 05/07/2019 17:06:10