I wonder why gcc (4.6.3) gives me no warning for the unreachable code in this example:
#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
", status());
printf("second call %d
", status());
return 0;
}
Note, the purpose of the faulty status()
function was to maintain a status. I had expected to get a warning for this with -Wall
. I tried also -Wunreachable-code
, -Wextra
, -pedantic
and -ansi
(as it was discussed here). Yet, none of those give me a warning.
It appears gcc silently removes the static variable assignment.
In my opinion gcc options -Wall -Werror
should throw an error.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…