c中的什么scanf函数返回?

Rac*_*hit 24 c scanf

我知道签名是

int scanf(const char *format, ...)
Run Code Online (Sandbox Code Playgroud)

这个int值与什么有关?

NPE*_*NPE 40

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.
Run Code Online (Sandbox Code Playgroud)

在你的情况下,scanf()可以返回0,1EOF.

PS正如其他人所指出的那样,你在前面错过了一个&符号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.
Run Code Online (Sandbox Code Playgroud)

没有&符号,代码的行为是不确定的.

  • [`scanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html)返回成功扫描并分配的项目数。如果格式字符串是`“%s%d%f%* s%n%d”`,则在一切正常的情况下返回4。`%* s`抑制分配,因此不计入,而`%n`返回偏移量且不计入。如果得到0、1、2或3,则出问题了。仅当没有剩余数据可读取或输入错误(不是格式错误,而是“硬件”错误)时,您才返回EOF。使用“%d”格式时,只有一次转换,因此您将获得“ EOF”,“ 0”或“ 1”。 (2认同)

gli*_*ite 8

来自scanf:

成功时,该函数返回成功读取的项目数.如果发生匹配故障,此计数可以匹配预期的读数或更少,甚至为零.如果在成功读取任何数据之前输入失败,则返回EOF.