abelenky
abelenky

Reputation: 64730

A Visual Studio 2010 Bug?

I think I found a bug in VS2010 (C / C++), but it seems so obvious, I cannot believe it.
(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");
    }
}

Upvotes: 3

Views: 223

Answers (2)

djechlin
djechlin

Reputation: 60848

To test this you should try compiling the code in a different compiler. Using gcc (without the -Wall -Wextra -Wpedantic flags):

$ 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.

Upvotes: 0

SLaks
SLaks

Reputation: 888223

int i=0, x = 10;

You just declared a second x variable scoped to the for loop.

The outer x variable is not affected.

Upvotes: 25

Related Questions