我知道scanf
函数的签名是:
int scanf(const char *format, ...)
这个函数返回的int
值是多少?
发布于 2012-05-06 17:58:49
在man
页面中:
NAME
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf
...
RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.
The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set indicate the
error.
在您的示例中,scanf()
可以返回0
、1
或EOF
。
发布于 2012-05-06 18:06:16
我认为您的代码无法正常工作,因为您忘记了scanf函数中的"&“。
int g=0; //init the variable
int p=scanf("%d",&g);
scanf函数将把输入的值放入g变量地址。
发布于 2012-05-06 18:10:25
从技术上讲,这是UB (未定义的行为)。
int g;
int p=scanf("%d",g);
^
将一个单一化的整数传递给scanf,以将其用作要写入的地址。从现在开始,任何事情都有可能发生。你的应用程序很可能会崩溃。
https://stackoverflow.com/questions/10469643
复制相似问题