简体   繁体   English

一个Visual Studio 2010 Bug?

[英]A Visual Studio 2010 Bug?

I think I found a bug in VS2010 (C / C++), but it seems so obvious, I cannot believe it. 我想我在VS2010(C / C ++)中发现了一个错误,但它看起来很明显,我无法相信。
(in the vein of Select isn't Broken ). 选择的静脉不是破碎的 )。

Please let me know if this is a bug, or if I'm missing something: 如果这是一个错误,或者如果我遗漏了某些内容,请告诉我:

int main(void)
{
    int x;  // Declare a variable x;

    for(int i=0, x = 10; i<5; ++i) // Initialize X to 10.  No way around this.
    {
        printf("i is %d\n", i);
    }

    if (x == 10) // warning C4700: uninitialized local variable 'x' used    
    {
        printf("x is ten\n");
    }
}
int i=0, x = 10;

You just declared a second x variable scoped to the for loop. 您刚刚声明了第二个x变量作为for循环。

The outer x variable is not affected. 外部x变量不受影响。

To test this you should try compiling the code in a different compiler. 要测试它,您应该尝试在不同的编译器中编译代码。 Using gcc (without the -Wall -Wextra -Wpedantic flags): 使用gcc(不带-Wall -Wextra -Wpedantic标志):

$ gcc a.c
a.c: In function ‘main’:
a.c:7: error: redeclaration of ‘x’ with no linkage
a.c:5: error: previous declaration of ‘x’ was here
a.c:7: error: ‘for’ loop initial declaration used outside C99 mode
a.c:9: warning: too few arguments for format
a.c:9: warning: too few arguments for format

As stated, the issue is you declared a different variable with tighter scope in the for loop. 如上所述,问题是您在for循环中声明了一个不同的变量,范围更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM