我想知道为什么gcc(4.6.3)没有给我这个例子中无法访问的代码的警告:
#include <stdio.h>
int status(void)
{
static int first_time = 1;
if (first_time) {
return 1;
first_time = 0; /* never reached */
} else {
return 0;
}
}
int main(int argc, const char *argv[])
{
printf("first call %d\n", status());
printf("second call %d\n", status());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意,故障status()功能的目的是保持状态.我曾期望得到一个警告-Wall.我也试过了-Wunreachable-code,-Wextra(-pedantic和这里-ansi讨论的那样).然而,这些都没有给我一个警告.
看来gcc会默默地删除静态变量赋值.
在我看来,gcc选项-Wall -Werror应该抛出一个错误.