• C Programming Video Tutorials

Static Keyword in C



In C language, 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.

Static Variable in C

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

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: %d\n", x);
   x++;
   printf("Incremented value of x: %d\n", 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 Function

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 1

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 2

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 3

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 : %d\n", n1, val);
   voidfn();		// Call intfn static function
   val = intfn(n1);
   printf("The square root of the %d : %d\n", 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
Advertisements