Static is a keyword in C language that can be used for variables and functions, meaning we can declare static variables and static functions. The scope of ordinary variables is limited to the range where they are defined, while static variables have a scope that extends throughout the program.
The static keyword can be used in the following situations
When a global variable is declared using the static keyword, it is called a static global variable. It is declared at the top of the program and its visibility is throughout the entire program.
When a function is declared using the static keyword, it is called a static function. Its lifetime is the entire program.
When a local variable is declared using the static keyword, it is called a static local variable. The memory for static local variables is valid throughout the entire program, but the visibility of the variable is the same as that of automatic local variables. However, when the static local variable’s value is modified during the first call of the function, the modified value will be available during the next function call.
👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection
When the static keyword is used to declare member variables in a class, they are called static member variables. They can be accessed by all instances of the class, rather than a specific instance.
Member functions declared with the static keyword in a class are called static methods. They can be accessed by all instances of the class, rather than a specific instance.
Let’s understand through an example.
#include <stdio.h>
int main(){ printf("%d", func()); printf("\n%d", func());
return 0;}
int func(){ static int count = 0; count++; return count;}
In the above code, we declare the count variable as static. When func() is called, the value of count is updated to 1, and during the next function call, the value of count becomes 2. Therefore, we can say that the value of static variables persists between function calls.
Static Functions
A static function in C language is a function that can only be accessed within the file that defines it. It has limited scope and cannot be accessed by other source files. Static functions are declared using the static keyword before the return type in the function declaration.
static return_type function_name(parameters){// function body}
static void func(){printf("Hello, World!");}
In the above example, the func() function is declared as a static function. It can only be accessed within the file that defines it.
Differences Between Static Variables and Global Variables
-
Scope: Global variables can be accessed throughout the entire program, including other source files, while static variables are limited to the source file where they are defined and cannot be accessed by other source files.
-
Accessibility: Global variables can be accessed externally, while static variables cannot be accessed outside the file where they are defined.
-
Memory Allocation: Global variables are allocated memory at the start of the program and destroyed at the end, while static variables are allocated memory during function calls and retain their values between calls.
Differences Between Static Local Variables and Static Global Variables
-
Scope: Static local variables have the same scope as automatic local variables, limited to the block where they are defined, while static global variables have file scope.
-
Memory Allocation: Static local variables are allocated memory during function calls and retain their values between calls, while static global variables are allocated memory at the start of the program and exist throughout program execution.
-
Accessibility: Static local variables cannot be accessed outside the function where they are defined, while static global variables can be accessed by other functions within the same file.
Note: It is important to distinguish between static variables and global variables, as well as between static local variables and static global variables.
Programmer Technical Communication Group
Scan the QR code to join the group and remember to note: city, nickname, and technical direction.