首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >scanf函数返回什么?

scanf函数返回什么?
EN

Stack Overflow用户
提问于 2012-05-06 17:57:08
回答 5查看 106.5K关注 0票数 31

我知道scanf函数的签名是:

int scanf(const char *format, ...)

这个函数返回的int值是多少?

EN

回答 5

Stack Overflow用户

发布于 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()可以返回01EOF

票数 47
EN

Stack Overflow用户

发布于 2012-05-06 18:06:16

我认为您的代码无法正常工作,因为您忘记了scanf函数中的"&“。

int g=0; //init the variable
int p=scanf("%d",&g);

scanf函数将把输入的值放入g变量地址。

票数 0
EN

Stack Overflow用户

发布于 2012-05-06 18:10:25

从技术上讲,这是UB (未定义的行为)。

int g;
int p=scanf("%d",g);
                 ^

将一个单一化的整数传递给scanf,以将其用作要写入的地址。从现在开始,任何事情都有可能发生。你的应用程序很可能会崩溃。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10469643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档