Question

I am learning storage classes in C.I have a simple code

enter code here
int f1()
{
    static int i=0;
    i++;
    printf("%d",i);
}

int f2()
{
    printf("%d",i);
}

int main()
{
    f1();f2();f1();f2();
}

Compiler gives error as 'i' is undeclared in f2(). As I thought,memory static variables are allocated in data section of program memory.So any function in that file should be able to access it.

How does compiler knows that variable locally declared in function is bounded to that function only?How compiler evaluates that?

Was it helpful?

Solution

Although the lifetime of static variable isn't tied to the scope where it is defined (unlike variables with automatic storage duration):

{
    static int i=0;
    i++;
    ...
    {
        i++;  // <-- still well defined, even in nested scope
    }
}
i++;  // <-- undefined

it is accessible only within this scope. The compiler just checks whether the symbol i has been defined before and it sees, that i has not been defined within that scope (the static int i=0; defines a variable that is accessible locally ~ compiler doesn't care about its lifetime).

In case you need it to be accessible out of its scope, you'll have to pass it out of it by reference (its address) or make it global:

static int i = 0;
...
{
    i++;
}
...
i++;  // <-- accessing global variable

OTHER TIPS

static variables are indeed stored in the data section but are only in the scope of the function they are declared in.

You should do the following

static int i=0;
int f1()
{
    i++;
    printf("%d",i);
}

int f2()
{
    printf("%d",i);
}

now the variable i can be accessed by both functions.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top