自从我多年前意识到这一点,默认情况下这不会产生错误(至少在GCC中),我一直想知道为什么?
我知道您可以发出编译器标志来产生警告,但是它不应该总是出错吗?为什么非void函数没有返回值才有效?
评论中要求的示例:
#include <stdio.h>
int stringSize()
{
}
int main()
{
char cstring[5];
printf( "the last char is: %c\n", cstring[stringSize()-1] );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...编译.
我看到签名中带有void返回的函数/方法在函数末尾有一个return语句.这是什么原因,这是否适用于其他语言?
据我所知,如果我想退出函数的末尾,我可以使用return.
AC示例:
void function(void)
{
int x = 1 + 2;
return; // what do we need this for, if at all?
}
Run Code Online (Sandbox Code Playgroud)