C - 使用-Wall编译不会警告未初始化的变量

Nic*_*son 3 c llvm-gcc

我有一个示例有缺陷的程序,应该给出一个关于未初始化变量的警告,但是当我编译它时,gcc不会给我任何警告.

这是代码:

#include <stdio.h>

int main()
{
    int foo;

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

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我运行的: cc -Wall testcase.c -o testcase

我得不到反馈.据我所知,这应该产生:

testcase.c: In function 'main': 
testcase.c:7: warning: 'foo' is used uninitialized in this function
Run Code Online (Sandbox Code Playgroud)

它似乎在他的C教程中以类似的例子正确警告Zed Shaw .这是我第一次尝试的例子,并注意到它没有按预期工作.

有任何想法吗?

编辑:

gcc的版本:

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

Gre*_*ill 7

您是否正在编译并启用了优化?这是我的man gcc页面所说的内容:

  -Wuninitialized
       Warn if an automatic variable is used without first being
       initialized or if a variable may be clobbered by a "setjmp" call.

      These warnings are possible only in optimizing compilation, because
       they require data flow information that is computed only when
       optimizing.  If you do not specify -O, you will not get these
       warnings. Instead, GCC will issue a warning about -Wuninitialized
       requiring -O.
Run Code Online (Sandbox Code Playgroud)

我的gcc版本是:

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

实际上,我只是在gcc 4.4.5上试过这个,我确实得到了警告而没有使用-O.所以这取决于你的编译器版本.