Contents
What is Static in C?
Static is a keyword used in the C programming language. It can be used with variables and functions as we can declare static variables and static functions. Ordinary variables are limited to the definition of their meaning, while the capability of static variables is the entire program.
Static keyword can be used in the following situations
-
Static global variables
When a global variable is declared with the static keyword, it is called a public static variable. He was announced at the beginning of the program and his comments spread throughout the program.
Example
#include <stdio.h>
static int static_var = 10;
int main() {
printf(“Static Variable: %d\n”, static_var);Â // Output: Static Variable: 10
return 0;
}
Output
Static Variable: 10
-
Static functions
When a function is declared using the static keyword, it is called a static function. Lifecycle across all services.
Example
#include <stdio.h>
static void static_func() {
printf(“This is a static function\n”);
}
int main() {
static_func();Â // Output: This is a static function
return 0;
}
Output
This is a static function
-
Static local variables
When a local variable is declared with the static keyword, it is called a static local variable. The memory of a static local variable is used throughout the program, but the appearance of the variable is the same as that of an automatic local variable. However, when the function updates the local variable the first time it is called, the changed value will be available in subsequent calls.
Example
#include <stdio.h>
void func() {
static int count = 0;Â // Static local variable
count++;
printf(“Count: %d\n”, count);
}
int main() {
func();Â // Output: Count: 1
func();Â // Output: Count: 2
func();Â // Output: Count: 3
return 0;
}
Output
Count: 1
Count: 2
Count: 3
-
Static member variables
When a variable member in a group is declared with the static keyword, it is called a similar member. These are accessible not from one instance, but from all instances of the category.
Example
#include <stdio.h>
struct Example {
static int static_member;
};
int Example::static_member = 10;
int main() {
printf(“Static Member: %d\n”, Example::static_member);Â // Output: Static Member: 10
return 0;
}
Output
Static Member: 10
-
Static methods
Member functions of a class are declared with the static keyword called static method. It is accessible from all instances of the class, not from a specific instance.
Example
#include <stdio.h>
class Example {
public:
static void static_method() {
printf(“This is a static method\n”);
}
};
int main() {
Example::static_method();Â // Output: This is a static method
return 0;
}
Output
This is a static method
Differences b/w static and global variable
Global variables are variables declared outside the function. These global changes are available at the beginning of the program and remain in effect until the end of the program. It can also be accessed from outside the program.
Static variables are limited to the document that defines them, meaning they cannot be accessed by other documents.
Both static variables and global variables have static initialization. Static initialization here means that if we do not assign a value to the variable, the value 0 will be assigned to the variable by default.
Differences b/w static local and static global variable
-
Static global variables
If a variable is declared with the static keyword outside a function, it is called a public static variable. It is accessible throughout the program.
-
Static local variables
Variables are declared with the static keyword in a function called static local variables. Local variables are similar to automatic local variables, but they reside and are scoped within the execution. When a function changes the value of a local static variable during a call, it will remain unchanged even during the next call.
Properties of a static variable
- For static variables, memory is allocated in the static variable.
- Memory will be available throughout the program, but the resource will remain the same as the automatic local variable. This The value
- will persist throughout the call.
- If we do not assign a value to the variable, the default value is 0.
- Global variables cannot be accessed from outside the program, but global variables can be accessed from other files.