static storage class specifier
static variables have a property of preserving their value even after they are out of their scope. This kind of variables is initialized only once and exist until the termination of the program.
Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default the initial value assigned the value 0 by the compiler.
#include <stdio.h> void abc() { int a=5; static int s=5; ++a; ++s; printf("\na=%d s=%d",a,s); } int main() { abc(); abc(); abc(); return 0; }
[quote]
Output:
a=6 s=6
a=6 s=7
a=6 s=8
[/quote]
Analysis
By default, any type of variable storage class specifier is ‘auto‘ and scoper and lifetime both are inside the block so it will create every time when control will pass to abc() where has ‘static‘ will be created only once and it remains there value until the end of the program.
#include <stdio.h> void abc() { static int s=5; ++s; printf("\nstatic1:%d",s); { static int s=10; ++s; printf("\nstatic2:%d",s); } } int main() { abc(); abc(); abc(); return 0; }
[quote]
Output:
static1:6
static2:11
static1:7
static2:12
static1:8
static2:13
[/quote]
Analysis
It’s possible to create a ‘static‘ variable within function body scope or sub-scope of any function. In this case, the static variable will create according to there scope and remains the same behavior as a normal static variable.
#include <stdio.h> void abc() { static int s=5; ++s; printf("s=%d",s); } int main() { abc(); abc(); printf("static data:%d",s); return 0; }
[quote]Output: Error undefined symbol ‘s’ [/quote]
Analysis
The scope of static variable is restricted within the function so abc() related static variable can’t access in main()
extern storage class specifier
When we are defining any variable outside of any function then it becomes a global variable. The global variable doesn’t require any storage class specifier and the default value is 0.
#include <stdio.h> int g; void abc() { ++g; } void xyz() { ++g; } void main() { ++g; abc(); xyz(); printf("g=%d",g); }
[quote]Output: g=3[/quote]
An extern storage class simply tells that the variable is defined elsewhere and not within the same block where it is used.
Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere.
It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block.
#include <stdio.h> void abc() { ++g; } int g=5; void xyz() { ++g; } void main() { ++g; abc(); xyz(); printf("a=%d",g); }
[quote]O/P: Error undefined symbol ‘g’[/quote]
Analysis
A global variable can be accessed within any function/block but if we are using that global variable before defining then it must be declared first using ‘extern’ keyword.
This declaration tells to the compiler that that variable defined somewhere else so pass the competition issues without any errors.
#include <stdio.h> void abc() { extern int g; //declatation ++g; } int g=5; //defining void xyz() { ++g; } int main() { ++g; abc(); xyz(); printf("a=%d",g); return 0; }
[quote]O/P:g=8[/quote]
#include <stdio.h> extern int g; //global declaration void abc() { //extern int g; local declaration ++g; } void xyz() { //extern int g; local declaration ++g; } int main() { //extern int g; local declaration ++g; abc(); xyz(); printf("g=%d",g); return 0; } int g;
[quote]O/P:g=3[/quote]
#include <stdio.h> extern int g; void abc() { --g; } int main() { --g; abc(); printf("g=%d",g); return 0; }
[quote]O/P: Error undefined symbol _g(Linking time)[/quote]
Analysis
As we discussed early ‘extern‘ keyword just bypass the compilation error only and doesn’t create any memory for the global variable. In above the compiler successfully compiles the program but we will get an error at linking time because global variable doesn’t have any definition.
#include <stdio.h> extern int g=10; void abc() { --g; } int main() { --g; abc(); printf("g=%d",g); return 0; }
[quote]O/P: g=8[/quote]
#include <stdio.h> extern int g=5; void abc() { ++g; } int main() { ++g; abc(); printf("g=%d",g); return 0; } int g=10;
[quote]O/P: Error Variable ‘g’ is initialized more than once[/quote]