Static in C Language

Understanding of Static in C Language

Static is a keyword employed in the C programming language, serving a dual role by associating with both variables

and functions. This means we can declare both static variables and static functions. While an ordinary variable’s scope is confined to where it is defined, a static variable’s scope spans the entire program. In C programming, you can use the “static” keyword to create a static variable that retains its value between function calls.

The static keyword is utilized in the following scenarios:

  1. Static Global Variable:
    When a global variable is declared with the static keyword, it becomes a static global variable. This declaration typically occurs at the program’s outset, and the variable’s visibility extends throughout the program.
  2. Static Function:
    A function declared with the static keyword is known as a static function. Its lifespan encompasses the entire program.
  3. Static Local Variable:
    A local variable declared with the static keyword becomes a static local variable. While its memory persists throughout the program, its visibility scope is akin to that of automatic local variables. Notably, when a function modifies a static local variable during its initial call, the modified value remains accessible during subsequent calls.
  4. Static Member Variables:
    Member variables declared with the static keyword within a class are referred to as static member variables. These variables can be accessed by all instances of the class, rather than being tied to a specific instance.
  5. Static Method:
    A member function within a class declared with the static keyword is known as a static method. Like static member variables, it is accessible by all instances of the class, not bound to a particular instance.

Let’s illustrate this with an example:

#include <stdio.h>

int main() {
    printf("%d", func());
    printf("\n%d", func());
    return 0;
}

int func() {
    static int count = 0; // variable initialization
    count++; // incrementing counter variable
    return count;
}

In the above code, the func() function is called, and the count variable gets updated. When the function completes its execution, the memory allocated for count is not released. This behavior is due to the variable being declared as static.

Output:

1
2

Static Variable:

A static variable is one that retains its value across multiple function calls.

Static Variable Syntax:

The syntax for declaring a static variable is as follows:

static data_type variable_name;

Consider this simple example of a static variable:

#include <stdio.h>

int main() {
    printf("%d", func());
    printf("\n%d", func());
    return 0;
}

int func() {
    static int count = 0;
    count++;
    return count;
}

In this code, the count variable is declared as static. When func() is called, the value of count persists across function calls.

Output:

1
2

Static Function in C:

Non-static functions are global by default, meaning they can be accessed outside the file. However, if a function is declared as static, its scope is limited to the file where it is defined. A static function might look like this:

static void func() {
    printf("Hello, World!");
}

Differences between Static and Global Variables in C:

Global variables are declared outside functions and have a scope that spans the entire program. They can be accessed outside the program as well.

Static variables are limited to the source file in which they are defined and are not accessible by other source files. Both static and global variables have static initialization, meaning if no value is assigned, the default value is 0.

Differences between Static Local and Static Global Variables in C:

Static Global Variable in C:

  • Declared with the static keyword outside a function.
  • Accessible throughout the program.

Static Local Variable in C:

  • Declared with the static keyword inside a function.
  • Scope is the same as automatic local variables.
  • Memory persists throughout program execution.
  • Modifications during one function call persist in subsequent calls.

Properties of a Static Variable in C:

  • Memory is allocated within the static variable.
  • Memory is available throughout the program, with a scope akin to automatic local variables.
  • Default value is 0.
  • Global static variables cannot be accessed outside the program, whereas global variables can be accessed by other source files.

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading