Always remember that the Scope is compile time not run-time. C has a flat memory structure. This means you can access anything from anywhere. You can make a pointer of i and can access it. But, C says undefined behaviour when the scope of the variable is over. This is a compiler restriction completely. You can also see the link - Is scope in C related only to compile time, as we know we can access any memory at run time? for more details. Also, this could be helpful A static variable and a global variable both reside in data segment. Still, static variable has scope limited. Why?. So, it is the translation unit that throws you the error.
Let's understand this by example.
#include "stdio.h"
int *ptr_i;
void func1()
{
static int i = 0;
ptr_i = &i;
i++;
printf("The static i=%d\r\n",i);
}
int main(int argc, char *argv[])
{
func1();
(*ptr_i)++;
func1();
}
The output of this program is the following.
The static i=1
The static i=3
As you could have understood, scope is not a run time. I was able to access the memory location used by i via pointer. Thus, you can access any memory in C as it is a flat memory structure. In this example, I accessed the memory of i using pointer to i. Note, Compiler never throw any error. Thus, scope is compile time not run time.