C语言 C 警告“返回”没有值,在函数中返回非空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2422636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C warning 'return' with no value, in function returning non-void
提问by ambika
I have this warning.
我有这个警告。
warning : 'return' with no value, in function returning non-void.
警告:“返回”没有值,在返回非空的函数中。
回答by Jonathan Leffler
You have something like:
你有这样的事情:
int function(void)
{
return;
}
Add a return value, or change the return type to void.
添加返回值,或将返回类型更改为 void。
The error message is very clear:
错误信息非常清楚:
warning : 'return' with no value, in function returning non-void.
警告:“返回”没有值,在返回非空的函数中。
A return with no value is similar to what I showed. The message also tells you that if the function returns 'void', it would not give the warning. But because the function is supposed to return a value but your 'return' statement didn't, you have a problem.
没有价值的回报与我展示的相似。该消息还告诉您,如果该函数返回“void”,则不会发出警告。但是因为该函数应该返回一个值,而您的“返回”语句却没有,所以您遇到了问题。
This is often indicative of ancient code. In the days before the C89 standard, compilers did not necessarily support 'void'. The accepted style was then:
这通常表示古代代码。在 C89 标准之前的日子里,编译器不一定支持“void”。当时接受的风格是:
function_not_returning_an_explicit_value(i)
char *i;
{
...
if (...something...)
return;
}
Technically, the function returns an int, but no value was expected. This style of code elicits the warning you got - and C99 officially outlaws it, but compilers continue to accept it for reasons of backwards compatibility.
从技术上讲,该函数返回一个int,但没有预期的值。这种代码风格会引发您收到的警告——C99 正式将其取缔,但出于向后兼容性的原因,编译器继续接受它。
回答by Robert Menteer
This warning also happens if you forget to add a return statement as the last statement:
如果您忘记添加 return 语句作为最后一条语句,也会发生此警告:
int func(){}
If you don't specify the return type of a function it defaults to int not to void so these are also errors:
如果你没有指定函数的返回类型,它默认为 int 而不是 void,所以这些也是错误:
func(){}
func(){ return; }
If you really do not need to return a value you should declare your function as returning void:
如果你真的不需要返回一个值,你应该将你的函数声明为返回 void:
void func(){}
void func(){ return; }
回答by Chris Lutz
This warning happens when you do this:
执行以下操作时会发生此警告:
int t() { return; }
Because t()is declared to return an int, but the return statement isn't returning an int. The correct version is:
因为t()被声明为返回一个int,但返回语句没有返回一个int。正确的版本是:
int t() { return 0; }
Obviously your code is more complicated, but it should be fairly easy to spot a bare returnin your code.
显然您的代码更复杂,但应该很容易return在您的代码中发现一个裸露的部分。

