Spark
Spark

Reputation: 216

Why I'm Getting Garbage Value as output?

Why the output is not 10 or even 5??

void main()
{
    int a=10;
    goto here;
    {
    int a=5;
    here:
        printf("%i",a);
    }
}

output: Garbage Value

Upvotes: 3

Views: 829

Answers (1)

Quimby
Quimby

Reputation: 19223

Because there are two a variables, the second shadows the first in the print statement. Since you skipped its initialization, the output is garbage.

Note it is a compiler error to skip past initialization in C++, in C you just get the uninitialized value as you have observed.

Also, it is int main(), not void main().

Upvotes: 14

Related Questions