Search code examples
cllvm-gcc

C - Compiling with -Wall doesn't warn about uninitialized variables


I have an example flawed program that should give exactly one warning about an uninitialized variable, but when I compile it gcc doesn't give me any warnings.

Here is the code:

#include <stdio.h>

int main()
{
    int foo;

    printf("I am a number: %d \n", foo);

    return 0;
}

Here is what I run: cc -Wall testcase.c -o testcase

And I get no feedback. As far as I know this should produce:

testcase.c: In function 'main': 
testcase.c:7: warning: 'foo' is used uninitialized in this function

It appears to warn Zed Shaw correctly in a similar example in his C tutorial). This is the example I had first tried and noticed that it wasn't working as expected.

Any ideas?

EDIT:

Version of gcc:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Solution

  • Use Clang, be done with it. Seems like a bug in GCC, cause Clang warns like it should.