Why doesn't GCC (4.6.3) give me any warning for the unreachable code in the below 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\n", status());
printf("second call %d\n", 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.