I'm learning the modifiers of variable in C++. I see this code in the tutorial:
int func()
{
static int number = 1;
return ++number;
}
int main()
{
int i = func();
printf("i is %d", i); //This gives 2
i = func();
printf("i is %d", i); //This gives 3
return 0;
}
From my understanding, I think the first time func()
is called, it defines the variable in the global space. However, the next time func()
is called, it will try to do the same thing in the global space, but why it doesn't result in a redefinition error?