Skip to content
Marantess edited this page Jan 11, 2019 · 2 revisions

Static

Static Variables in C

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Syntax:

static data_type var_name = var_value;

A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. For example, we can use static int to count number of times a function is called, but an auto variable can’t be sued for this purpose.

#include<stdio.h> 

int fun() 
{ 
  static int count = 0; // count is a statc int
  count++; 
  return count; 
} 
   
int main() 
{ 
  printf("%d ", fun()); 
  printf("%d ", fun()); 
  return 0; 
}

Output:

1 2 

However, if count were declared simply as int, we would have a different ouput. So, let's see:

#include<stdio.h> 

int fun() 
{ 
  int count = 0; // count is not static anymore
  count++; 
  return count; 
} 
   
int main() 
{ 
  printf("%d ", fun()); 
  printf("%d ", fun()); 
  return 0; 
}

Output:

1 1 

Static variables are allocated memory in data segment, not stack segment.

What are the default values of static variables in C?

In C, if an object that has static storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a NULL pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these rules.

To get a better understanding, look at the following example:

#include<stdio.h>

int g;  //g = 0, global objects have static storage duration 
static int gs; //gs = 0, global static objects have static storage duration 
int main() 
{ 
  static int s; //s = 0, static objects have static storage duration 
  printf("Value of g = %d", g); 
  printf("\nValue of gs = %d", gs); 
  printf("\nValue of s = %d", s); 
  
  getchar(); 
  return 0; 
} 

Output:

Value of g = 0
Value of gs = 0
Value of s = 0

Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage.

#include <stdio.h>

int main() 
{ 
    static int x; 
    int y; 
    printf("%d \n %d", x, y); 
} 

Output:

0 
[some_garbage_value] 

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. See this for more details.

#include<stdio.h> 

int initializer(void) 
{ 
    return 50; 
} 
   
int main() 
{ 
    static int i = initializer(); 
    printf(" value of i = %d", i); 
    getchar(); 
    return 0; 
}

Output:

In function 'main':
9:5: error: initializer element is not constant
     static int i = initializer();
     ^

Static Functions in C

In C, functions are global by default. The static keyword before a function name makes it static. For example, below function foo() is static.

static int foo(void) 
{ 
  printf("I am a static function "); 
} 

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.