What is the output of this C code?
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
Answer: A
Correct answer: 6 7
A static local is initialised once, before the program starts, and keeps its value between calls - its storage lasts as long as the program even though its name is local. So the first call prints 6 and the second 7. An ordinary local would print 6 both times.
Please log in or register to report this question.