求问一个scanf函数的问题 求教!!!
本帖最后由 u011091754 于 2013-06-19 23:31:32 编辑
#include <stdio.h>
int main()
{
int x,y;
x=scanf("%d",&y);
printf("%d,%d\n",x,y);
return 0;
}
我想问的是为什么输入数字x得1 输入字母x得0呢
请教 C
[解决办法]
scanf的返回值指明成功输入了多少个数据,如果为EOF则说明读到了文件尾或是文件IO错。
代码中的"%d"说明要求输入整数,你输入数字时,输入成功,数据量为1;你输入字母时,它不是有效的整数,输入的数据量为0。
[解决办法]
在这方面scanf 与printf 不一样。要是printf 的话,结果都会一样,只要是%d 都会是数字。
[解决办法]
%d为整形,应该按格式输入
[解决办法]
x是scanf的返回,输入成功一个,返回1,成功两个返回2。
scanf是%d,只能输入数字才能成功返回,否则返回错误。就是0
[解决办法]
scanf, wscanf
Read formatted data from the standard input stream.
int scanf( const char *format [,argument]... );
int wscanf( const wchar_t *format [,argument]... );
Routine Required Header Compatibility
scanf <stdio.h> ANSI, Win 95, Win NT
wscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.
[解决办法]
x是scanf的返回值,如果输入争取(输入的是数字)则返回1,如果输入的是字母(非数字)则表示出错,返回0(或者EOF).
这是正常的。
[解决办法]