Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java Insert a String into another String in Java Print Shortest Path to Print a String on Screen in Java Search Insert Position in Java BST Sequences in Java Burrows - Wheeler Data Transform Algorithm in Java Convert BST to Min Heap in Java Fibonacci Sum in Java Get name of current method being executed in Java

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.