Question 11: Predict the output
int main()
{
int x = 0;
while(x++<5)
{
static int x;
x += 2;
std::cout<<x<<" ";
}
}
Options:
a) 1 2 3 4 5
b) 2 4 6 8 10
c) Compile Time error
d) Runtime Error
Answer: b
This explains the concept of scope of variables or more accurately the concept of local variables.
ReplyDeleteI would say it explains the scope of static variable.
DeleteTo see the difference remove static keyword from the above code and see the behavior.