#include <stdio.h>
int main()
{
int a = 10;
goto inside;
{
int a = 20;
{
inside:
printf("%d",a);
}
}
}
Why does it print output as 0, not as 10? Even if it looks for nearest scope ie. int a = 20
which is not executed clearly. The memory contains only int a = 10;
so why 0 here?
At the point of the printf
call, the name a
refers to the second variable of that name, the one that's initialized to 20
. The outer a
, the one initialized to 10
, is hidden. There's no reason for the printf
to print 10
.
The goto
statement skips over the initialization of the inner a
, so it's never initialized to 20
. Space is allocated for it, but its value is arbitrary garbage. The 0
being printed is the contents of the inner a
. It could have been anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With