I have recently answered a question on C, in which I suggested turning on all compiler warnings.
I thought that I would copy-paste the actual warning that I was sure gcc would have given on compiling the OP's code.
Except it did not work. I boiled it down to this test case which also does not work:
int main() {
int a; /* not initialised */
while (a) { /* warn me! */
a++;
}
return 0;
}
I would have bet my last penny on gcc
saying:
test.c: In function ‘main’:
test.c:4:6: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
while (a) {
^
...and I would have lost. I get no warning whatsoever! And yet if I use if
instead of while
, why yes, then I get exactly that warning. But with while
? Nary a whisper.
I checked out some reports that seemed germane and concluded that while they don't apply, there just might be something done by gcc
that makes the warning superfluous in just this one case. I'd like to know what.
Is it because a
is initialized to 0? No, because
int main() {
int a;
if (a == 0) { printf("a is zero\n"); }
while (a == 0) { a = printf("a is zero\n"); }
return 0;
}
gives an error on the if(), but gives no more errors if I comment the if() out. If gcc knew that a was going to be 0, the first if also should have elicited no error.
So what's happening here?