Consider the following program.
void main()
{
static int x = 10;
int i = 0;
for (i=0; i<2; i + +)
{
int x = 20;
x = x+1;
}
x=x+1;
printf("%d",x);
}
What is the output of the above program?
A
22
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
11
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
C
10
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
23
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution
The correct option is B 11 For loop block has variable 'x'. It has no effect on the outside for block on static variable x.
x = x+1 statement uses static variable x and variable x of for block scope completes and not available after the loop termination.
x = x+1 = 10+1 = 11
So, output 11 will be produced by the given code.