In C ,what is the use of static storage class when an external variable can serve its purpose at the same cost ie. both occupy storage space in the data segment of the execu
The basic difference is on scope of variable.
1) global variable is global for entire project. lets say your project has 10 different files then all 10 files can access the global variable(see how to use extern).
2) static variable/function can be used by function/file within which it is defined. It cannot be used by any other file in your project.
yet, you can modify the static variable(defined in func1()) in func2() by passing reference of the variable. please look into below example,
void func2(int *i)
{
(*i)++;
}
void func1()
{
static int i;
i=1;
printf("%d\n", i);
func2(&i);
printf("%d\n", i);
}
int main()
{
func1();
return 0;
}
As you see above, func1() has static int i which cannot be directly manipulated by func2(), but if you pass reference of the variable you can still manipulate the variable like ordinary variable.
hope it helps...