Static Member Variable

Static Keyword

It is used to control the visibility and persistence of members across different parts of a program.

What are Static Variables?

Static variables in C can be classified into two main categories based on their scope:

  1. Static Local Variables
  2. Static Global Variables

Static Local Variables

As we all know that the local variables have no linkage and have automatic duration, means they are only visible within the scope in which they are declared, created when the block is entered and destroyed when the block is exited.

Unlike regular local variables, which are reinitialized every time the function is called, static local variables retain their value between function calls, their duration is changed to the static duration, means they exist for the lifetime of the program.

A local variable has the following properties:

  • Automatic Duration:
    • Automatically created when the block in which they are declared is entered and destroyed when the block is exited.
  • Block | Local Scope:
    • Only accessible within the declared block.
  • No Linkage:
    • Only visible with in the scope in which it is declared.

However, when it is turned to static local variable, its properties is changed to:

  • Static Duration:
    • Exist for the entire lifetime of the program.
  • Block | Local Scope:
    • Only accessible within the declared block.
  • No Linkage:
    • Only visible with in the scope in which it is declared.

A static local variable is declared inside a function with the static keyword.

#include <stdio.h>

void counter() {
    static int count = 0; // Static local variable
    count++;
    printf("Count: %d\n", count);
}

int main() {
    counter(); // Output: Count: 1
    counter(); // Output: Count: 2
    counter(); // Output: Count: 3
    return 0;
}
// Output
Count: 1
Count: 2
Count: 3

In this example, the count variable retains its value between calls to the counter function. This behavior is useful when you need to maintain state information across multiple function invocations.

Note:

If we don't initialize the static local variable, it is initialized to 0 by default.

For Example:

#include <stdio.h>

void func() {
    static int count;  // Implicitly initialized to 0
    count++;           // Increment the value
    printf("Count: %d\n", count);
}

int main() {
    func();  // Output: Count: 1
    func();  // Output: Count: 2
    func();  // Output: Count: 3
    return 0;
}
// Output
Count: 1
Count: 2
Count: 3

Static Global Variables

As we know that Global Variables have the static duration (means these exist for the entire lifetime of the program), file | global scope (They are accessible from the point of declaration to the end of the file) and external linkage (can be accessed from other files using the extern keyword).

Global variable has the following properties:

  • Static Duration:
    • Exist for the entire lifetime of the program.
  • Global Scope:
    • Accessible from the point of declaration to the end of the file.
  • External Linkage:
    • Accessible from other files using the extern keyword.

However, using static keyword with global variables changes the linkage to internal means we can only access that variable within the same translation unit (file), we can't access it from outside translation unit (non-declaration file) using extern keyword.

With the introduction of static keyword with global variables its properties is changed to:

  • Static Duration:
    • Exist for the entire lifetime of the program.
  • Global Scope:
    • Accessible from the point of declaration to the end of the file.
  • Internal Linkage:
    • Only Visible within the current translation unit (in which it is declared). Now we can't access it from outside non-declaration files using extern keyword.

A static global variable is declared outside any function but with the static keyword. This restricts its visibility to the file in which it is declared, making it inaccessible from other files.

#include <stdio.h>

static int globalCount = 0; // Static global variable

void increment() {
    globalCount++;
    printf("Global Count: %d\n", globalCount);
}

int main() {
    increment(); // Output: Global Count: 1
    increment(); // Output: Global Count: 2
    return 0;
}

Here, the globalCount variable is only accessible within the file where it is declared. This encapsulation helps prevent naming conflicts and promotes modularity.

Benefits of Static Variables in C

  • Persistence: Static local variables maintain their values between function calls.
  • Encapsulation: Static global variables limit the scope of variables to a single file, reducing the risk of naming conflicts.
  • Memory Efficiency: Static variables are allocated memory only once, which can be beneficial for performance.
Variable TypeDurationScopeLinkage
Global Variable   
NormalStaticFile/GlobalExternal
StaticStaticFile/GlobalInternal
Local Variable   
NormalAutomatic (stack)Block/FunctionNone
StaticStaticBlock/FunctionNone