”;
What is static Keyword in C?
The static keyword in C is one of the storage class specifiers which has different meanings based on the context in which it is used.
The “static” keyword is used to declare a static variable as well as to define a static function. When declared as “static”, a variable represents a static storage class.
A static function is available only inside the program file (with “.c” extension) in which it is defined. One cannot use the “extern” keyword to import a static function into another file.
Uses of static Keyword
The following are the different uses of the static keyword
- Static Local Variable: When a local variable is declared with the static keyword, its lifetime will be till the end of the program and it retains the value between the function calls.
- Static Global Variable: When a global variable is declared with the static keyword, it can only be accessed within the same file. It is useful when you want to make a global variable as a private global variable to the file in which it is declared.
- Static Functions: When you function is declared as a static function, its scope will be limited to the file in which the function is declared. You cannot access the function in other files.
Static Variables (static Keyword with Variables)
When a variable is declared as static, it is initialized only once. The compiler persists with the variable till the end of the program. A static variable is also used to store data that should be shared between multiple functions.
Here are some of the important points to note regarding a static variable −
- The compiler allocates space to the static variable in computer’s main memory.
- Unlike auto, a static variable is initialized to zero and not garbage.
- A static variable is not re-initialized on every function call, if it is declared inside a function.
- A static variable has local scope.
Example of static Keyword with Variables
In the following example, the variable “x” in the counter() function is declared as static. It is initialized to “0” when the counter() function is called for the first time. On each subsequent call, it is not re-initialized; instead it retains the earlier value.
#include <stdio.h> int counter(); int main() { counter(); counter(); counter(); return 0; } int counter() { static int x; printf("Value of x as it enters the function: %dn", x); x++; printf("Incremented value of x: %dn", x); }
Output
When you run this code, it will produce the following output −
Value of x as it enters the function: 0 Incremented value of x: 1 Value of x as it enters the function: 1 Incremented value of x: 2 Value of x as it enters the function: 2 Incremented value of x: 3
A static variable is similar to a global variable, as both of them, are initialized to 0 (for numeric types) or null pointers (for pointers) unless explicitly assigned. However, the scope of the static variable is restricted to the function or block in which it is declared.
Static Functions (static Keyword with Functions)
By default, every function is treated as global function by the compiler. They can be accessed anywhere inside a program.
When prefixed with the keyword “static” in the definition, we get a static function that has a scope limited to its object file (the compiled version of a program saved with “.c” extension). This means that the static function is only visible in its object file.
A static function can be declared by placing the keyword “static” before the function name.
Example of static Keyword with Function (in Multiple Files)
Open a console application with the CodeBlocks IDE. Add two files “file1.c” and “main.c”. The contents of these files are given as follows −
Contents of “file1.c” −
static void staticFunc(void) { printf("Inside the static function staticFunc() "); }
Contents of “main.c” −
#include <stdio.h> #include <stdlib.h> int main() { staticFunc(); return 0; }
Now, if the above console application project is built, then we will get an error, i.e., “undefined reference to staticFunc()”. This happens as the function staticFunc() is a static function and it is only visible in its object file.
Example of static Keyword with Function (in the Same File)
The following program demonstrates how static functions work in a C program −
#include <stdio.h> static void staticFunc(void){ printf("Inside the static function staticFunc() "); } int main(){ staticFunc(); return 0; }
Output
The output of the above program is as follows −
Inside the static function staticFunc()
In the above program, the function staticFunc() is a static function that prints “Inside the static function staticFunc()”. The main() function calls staticFunc(). This program works correctly as the static function is called only from its own object file.
Example of static Keyword with Multiple Functions
You can have multiple static functions in the same object file, as illustrated in the following example −
#include <stdio.h> #include <stdlib.h> #include <math.h> // define the static function static int square( int num){ return num * num; } static void voidfn(){ printf ("From inside the static function.n"); } static int intfn(int num2){ return sqrt(num2); } int main(){ int n1, val; n1 = 16; val = square(n1); // Call voidfn static function printf("The square of the %d : %dn", n1, val); voidfn(); // Call intfn static function val = intfn(n1); printf("The square root of the %d : %dn", n1, val); return 0; }
Output
When you run this code, it will produce the following output −
The square of the 16: 256 From inside the static function. The square root of the 16: 4
”;