Static Keyword in C

Static Keyword in C


The static keyword is frequently used in C programming language. Programmers use this keyword without knowing the complete meaning of it. Let's see how and when to use this keyword in C program.


Declaring the variable as static:

The static keyword for variables is mainly used for the following reasons:
1. To restrict the visibility of the variable to the function/file where the variable is declared.
2. To retain the value stored in the variable even after the execution of the function is over. This means when a variable is declared as static within a function, the visibility of the variable is limited to the function, but the variable exists throughout the lifetime of the program.


For example: 
void func(void)
{
static int i =0;
i = i +1;
printf("%d\n", i);
}

int main(void)
{
func();
func();
return 0;
}

output: 1
2

If we observe in the above program, when the func( ) is called for the second time in the main program, the value of 'i' is remaining as 1 and it is incremented to 2.

Note: Always be careful when declaring the variable as static in a header file, this is because whenever the header file is included in different .c files, each new copy of the variable will be created.

Also it is very important to remember that a static variable should always be initialized with constant literal in C. This is because, the value of the static variable should be resolved before the execution of the main( ) function starts.

For Ex: int a = 100;
             static int b = a; // Is not allowed and compiler will throw an error.


Declaring the function as static:

Generally a function is declared as static in large projects because of the following reason:

There could be a chance that a function with the same name could be declared/used in some other part of the file. So in this scenario if it is identified that the function that you are going to define will be used in only one file then it is better to make the function as static. This solves the ambiguity when the compiler compiles the project. In short static function is not visible outside of a translation unit(same .c file).

Ex:
/*static_test.c*/
int f1(int);
static int f2(int);
int f1(int foo) {
return f2(foo);
}
int f2(int foo) {
return 42 + foo;
}
/*main.c*/
int f1(int);
int f2(int);
int main(void) {
f1(10); /* f1 is visible to the linker */
f2(12); /* f2 is not visible to the linker */
return 0;
}
If you could see in the above example, function f1 is visible to main.c as it is not made static in static_test.c, whereas f2() will not be visible to the linker and compilation will fail as the definition of the function f2() is not visible. 

That's all about a static keyword if you feel any correction is needed. Please let us know. 
Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...