Differences Between The Internal And External Static Variables In C

Static variables are a category of variables that maintain their value across multiple function calls. Unlike regular variables, which are initialized and destroyed with each function call, static variables persist throughout the entire runtime of the program. The "static" keyword is used to declare static variables.

There exist two categories of static variables: Internal and External static variables.

Internal static variables:

Static variables declared within a function are defined using the static keyword. These variables are confined to the function's scope in which they are declared and cannot be accessed or altered directly from outside the function. They are commonly referred to as local variables.

Example:

Let's consider a C program to demonstrate the usage of internal static variables:

Example

#include <stdio.h>

void magicRoom() {

 static int magicBox = 5;

 printf("Magic Box in the room: %d\n", magicBox);

 magicBox++;

}

int main() {

 magicRoom();

 magicRoom();

 magicRoom();

 return 0;

}

Output:

Output

Magic Box in the room: [value]

Explanation:

In this instance, the program contains two functions: magicRoom and the primary function. Within the magicRoom function, there is an internal static variable called magicBox that is set to an initial value of 5.

It displays the value stored in magicBox and increases it each time the magicRoom function is invoked thrice within the main function.

Internal static variables are confined within the scope of the magicRoom function, ensuring encapsulation and preventing direct access from external sources. Initially set to 5, the magicBox value increments upon the first function call. Subsequently, each function call results in printing the updated magicBox value along with an increment.

External static variables:

Global static variables in C are declared with the static keyword at the global scope. They can be accessed from any part of the program, not limited to specific functions. These variables maintain their values throughout the entire program runtime.

Example:

Let's consider a C program to demonstrate the usage of external static variables:

Example

#include <stdio.h>

static int globalMagicNumber = 10;

void mathClass() {

 printf("Global Magic Number in Math class: %d\n", globalMagicNumber);

}

void scienceClass() {

 printf("Global Magic Number in Science class: %d\n", globalMagicNumber);

}

int main() {

 printf("Global Magic Number in the main program: %d\n", globalMagicNumber);

 mathClass();

 scienceClass();

 return 0;

}

Output:

Output

Magic Box in the room: [value]

Explanation:

In this scenario, the significance of external static variables is demonstrated. Within the program, there exist three functions: mathClass, scienceClass, and main. At the beginning of the program, an external static variable, globalMagicNumber, is defined and set to 10. This variable is globally accessible within the program, hence it is utilized in the mathClass, scienceClass, and main functions. The value assigned to globalMagicNumber remains consistent throughout the entire program execution.

Differences between the Internal and external static variables:

There exist numerous distinctions between internal and external static variables. Here are some primary variances between internal and external static variables:

Feature Internal static variables External static variables
Declaration Location Declared inside a function or block. Declared outside of any function or block.
Scope Limited to the block or function. Available throughout the entire program.
Access within Functions Accessible only within the same source file. Accessible to all functions in the program.
Lifetime Exists only during the execution of the function. Exists throughout the entire program execution.
Data Hiding Encapsulates data within a specific compilation unit. Encapsulates data within the entire program.
Memory Allocation Allocated once, upon function entry. Allocated once during program initialization.
Collision Risk Low risk of naming conflicts within functions. Low risk of naming conflicts with other files.
Use Cases Suitable for temporary or local data. Suitable for data shared across multiple functions or files.
Reusability Enhances modularity within a specific function Enhances modularity and supports broader reuse
Inter-Function Communication Facilitates communication within a function. Facilitates communication across functions or files.
Compilation Units Encourages creating self-contained units. Encourages modular and reusable code units.
Linkage Type Internal linkage, visible only within the translation unit. External linkage, can be accessed by other translation units.

Conclusion:

Two distinct categories of static variables, namely internal and external, fulfill specific roles. Internal static variables are defined inside a function to encapsulate data, confining its visibility to that particular function. This practice enhances modularity and enforces data privacy. Conversely, external static variables are declared globally, enabling shared accessibility among functions within the same file.

Input Required

This code uses input(). Please provide values below: