自从我多年前意识到这一点,默认情况下这不会产生错误(至少在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)
...编译.
为什么这段代码打印出n-100?
int hello(int n)
{
for(int i = 0; i < n-100; i++)
{
}
}
int main()
{
int h = hello(12);
cout << hello(12) << " " << h << endl;
}
Run Code Online (Sandbox Code Playgroud)
然而,这两个函数都返回垃圾(分别为2665092和0)
int hello1(int n)
{
for(int i = 0; i < 12; i++);
}
int hello2(int n)
{
(n - 100);
}
Run Code Online (Sandbox Code Playgroud)
我在cygwin环境中使用g ++编译了这段代码.