简体   繁体   English

没有返回语句的Printf返回值

[英]Printf return value without return statement

Tried this on 3 different systems: CentOS, Ubuntu and Windows(MinGW) ; 在3个不同的系统上进行了尝试:CentOS,Ubuntu和Windows(MinGW); so I don't think this is an anomaly (undefined behavior) 所以我不认为这是异常现象(未定义的行为)

int hello(void)
{
        (void)printf("HELLO WORLD\n");
}

int main(void)
{
        printf("%d\n",hello());
        return 0;
}

Output is: 输出为:

HELLO WORLD 你好,世界
12 12

It returns the correct value, even with different text. 即使使用不同的文本,它也会返回正确的值。 Any explanations? 有什么解释吗?

Yes, it is undefined behaviour . 是的,这 未定义的行为

If the ending } is reached, without a return , using the returned value is UB. 如果结束}达到,没有return ,使用返回的值是UB。

From C11 , chapter §6.9.1, Function definitions C11 ,第§6.9.1章, 函数定义

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined. 如果到达终止函数的} ,并且调用者使用了函数调用的值,则该行为未定义。

You should not really try to judge the UB by seeing the result of UB. 您不应真正尝试通过查看UB的结果来判断 UB。 It is, well, undefined , after all. 毕竟,它是undefined

When you build your code, you may get a warning: 构建代码时,您可能会收到警告:

warning: no return statement in function returning non-void [-Wreturn-type]

In function 在功能上

int hello(void)
{
    (void)printf("HELLO WORLD\n");
}

Because of no return statement. 因为没有return声明。 That's why you will get Undefined Behavior 这就是为什么您会得到未定义的行为

While everyone points out the entire business is undefined behaviour, only one person answered to the actual question. 尽管每个人都指出整个业务都是不确定的行为,但只有一个人回答了实际问题。 However, there question is even more wrong and for some reason nobody tackles it. 但是,那里的问题更加错误,由于某种原因,没有人解决。

Tried this on 3 different systems: CentOS, Ubuntu and Windows(MinGW) ; 在3个不同的系统上进行了尝试:CentOS,Ubuntu和Windows(MinGW); so I don't think this is an anomaly (undefined behavior) 所以我不认为这是异常现象(未定义的行为)

This reasoning is seriously flawed (no amount of systems you test can tell you whether this is defined or not). 这种推理存在严重缺陷(您所测试的系统数量无法告诉您是否已定义)。 Not only that, your testing was clearly superficial. 不仅如此,您的测试显然是肤浅的。 On my system compiling the code with -O2 suddenly makes it print 0, and likely same things will happen on your system. 在我的系统上,使用-O2编译代码突然使它打印为0,并且可能在您的系统上发生同样的事情。

The reason it could have worked by accident is that on your system return value is kept in EAX which was not modified after return to main() and the compiler emitted code which moves the value from EAX to the first argument of another printf. 它可能偶然起作用的原因是,在您的系统上,返回值保留在EAX中,返回到main()之后并没有被修改,并且编译器发出了将值从EAX移至另一个printf的第一个参数的代码。

As was noted earlier and demonstrated with -O2 failure, this cannot be relied upon. 如前所述,并显示了-O2失败,因此不能依靠它。

Finally a sentence which is even more wrong: 最后是一个更错误的句子:

It returns the correct value, even with different text. 即使使用不同的文本,它也会返回正确的值。 Any explanations? 有什么解释吗?

What? 什么?

Let's take a look at the function in question: 让我们看一下有问题的函数:

int hello(void)
{
        (void)printf("HELLO WORLD\n");
}

The function has no return statement, so whatever result you get is not "the correct value". 该函数没有return语句,因此无论得到什么结果都不是“正确的值”。 In particular, you did not ask the language to return the value returned by printf. 特别是,您没有要求语言返回printf返回的值。 It so happened that in your testing you got the value returned by printf, but that's not what you asked for, hence it was not "correct" in any sense. 碰巧的是,在您的测试中,您获得了printf返回的值,但这不是您要的,因此从任何意义上说,它都不是“正确的”。 Strictly speaking one could wonder if the return value of the function which was called last is guaranteed to be returned in such a case, but it was explained and demonstrated that no. 严格来讲,人们可能想知道在这种情况下是否可以保证返回最后调用的函数的返回值,但是对此进行了解释并说明没有。

12 is exactly the return value of the first printf. 12恰好是第一个printf的返回值。 Probably, the result of printf("HELLO WORLD\\n"); 可能是printf("HELLO WORLD\\n"); will be left on EAX register. 将保留在EAX寄存器上。

So, when second printf expect a return value it is the last returned value of a function( EAX ). 因此,当第二个printf期望返回值时,它是函数( EAX )的最后一个返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM