Search code examples
c++cgoto

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


Solution

  • 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().