Internal static variable vs External static variables in C

There are two primary categories of static variables in C programming: internal static variables and external static variables. It is imperative that you understand the distinctions between these variables' scopes and lifetimes to write modular and effective code. In this article, we will discuss the difference between the internal static variable and external static variable in C. But before discussing the differences, we must know about the internal static and external variable in C.

Internal Static Variables:

  • Internal static variables hold their values in between function calls, and are declared inside a function or block.
  • They are defined by utilizing the static keyword within a function.
  • Due to their local scope, these variables can only be accessed inside the function in which they are declared.
  • Throughout the execution of the program, internal static variables are initialized just once, and their values hold true for all function calls.

Syntax:

It has the following syntax:

#include <stdio.h>

void exampleFunction() {

    static int internalStaticVar = 0;

    internalStaticVar++;

    printf("Internal Static Variable: %d\n", internalStaticVar);

}

int main() {

    exampleFunction();  // Output: Internal Static Variable: 1

    exampleFunction();  // Output: Internal Static Variable: 2

    return 0;

}

External Static Variables:

  • With a file scope, external static variables are declared outside of any function.
  • The static keyword is also used to define them, but file-level declaration is required.
  • External static variables are local to that file and invisible to other files in the program since they can only be accessed within the file in which they are defined.

Syntax:

It has the following syntax:

#include <stdio.h>

static int externalStaticVar = 0;

void exampleFunction() {

    externalStaticVar++;

    printf("External Static Variable: %d\n", externalStaticVar);

}

// File: main.c

#include <stdio.h>

extern void exampleFunction();  // Declaration to use the function from example.c

int main() {

    exampleFunction();  // Output: External Static Variable: 1

    exampleFunction();  // Output: External Static Variable: 2

    return 0;

}

Difference between Internal Static Variable and External Static Variable:

Definition:

  • Internal static variables are defined as variables declared inside a function using the static keyword.
  • External static variables are defined as variables declared using the static keyword outside of any function.

Scope:

  • The local scope of internal static variables within their defined function is clarified.
  • External static variable scope is made clear that they are only available within the file in which they are declared. It is known as file scope.

Life time:

  • The persistence of internal static variables is demonstrated by the fact that their values are retained in between function calls.
  • A longer lifetime is implied by the file scope of external static variables.

Example 1:

#include <stdio.h>

// Internal static variable

void exampleInternalFunction() {

    static int internalStaticVar = 0;

    internalStaticVar++;

    printf("Internal Static Variable (Function 1): %d\n", internalStaticVar);

}

// External static variable

static int externalStaticVar = 0;  // Declared outside of any function

void exampleExternalFunction() {

    externalStaticVar += 2;

    printf("External Static Variable (Function 2): %d\n", externalStaticVar);

}

int main() {

    // Using internal static variable within the same function

    exampleInternalFunction();  // Output: Internal Static Variable (Function 1): 1

    exampleInternalFunction();  // Output: Internal Static Variable (Function 1): 2

    // Using external static variable within a different function

    exampleExternalFunction();  // Output: External Static Variable (Function 2): 2

    exampleExternalFunction();  // Output: External Static Variable (Function 2): 4

    // Using internal static variable again

    exampleInternalFunction();  // Output: Internal Static Variable (Function 1): 3

    return 0;

}

Output:

Internal Static Variable (Function 1): 1

Internal Static Variable (Function 1): 2

External Static Variable (Function 2): 2

External Static Variable (Function 2): 4

Internal Static Variable (Function 1): 3

Example 2:

#include <stdio.h>

// External static variable with file scope

static int externalStaticVar = 0;

// Internal static variable within a function

void exampleFunction() {

    static int internalStaticVar = 0;

    internalStaticVar++;

    printf("Internal Static Variable (Function): %d\n", internalStaticVar);

}

// Another function that uses the external static variable

void anotherFunction() {

    externalStaticVar += 5;

    printf("External Static Variable (Another Function): %d\n", externalStaticVar);

}

int main() {

    // Using internal static variable within a function

    exampleFunction();  // Output: Internal Static Variable (Function): 1

    exampleFunction();  // Output: Internal Static Variable (Function): 2

    // Using external static variable in a different function

    anotherFunction();  // Output: External Static Variable (Another Function): 5

    anotherFunction();  // Output: External Static Variable (Another Function): 10

    // Using internal static variable again

    exampleFunction();  // Output: Internal Static Variable (Function): 3

    return 0;

}

Output:

Internal Static Variable (Function): 1

Internal Static Variable (Function): 2

External Static Variable (Another Function): 5

External Static Variable (Another Function): 10

Internal Static Variable (Function): 3

Head to head comparison between Internal Static Variable and External Static Variable:

ParametersInternal Static VariableExternal Static Variable
LinkageInternal static variable has internal linkage.External static variable has external linkage.
DeclarationInternal static variables are declared within a function.External static variables are declared outside a function.
VisibilityInternal static variables are accessible only in their particular function.External Static variables are accessible only from within the file of their declaration.
LifetimeThe storage duration of Internal static variables is throughout the whole program.The storage duration of external static variables is throughout the whole program.
ScopeFunction of the variable's declaration.File of the variable's declaration.

Conclusion:

In C, managing the scope and durability of data within a program can be accomplished through different techniques offered by internal and external static variables. Internal static variables have local scope, which allows them to hold onto their values in between function calls. They are defined within functions using the static keyword.

External static variables have file scope when they are declared outside of any function that uses the static keyword. It indicates that although they are hidden from other program files, they are accessible within the specified file. External static variables come into existence when a shared piece of data needs to be kept across several functions in the same file.