Static in C Language
0 347
In C Language, the static keyword has various meanings but its primary use is to control the Visibility and Lifetime of Variables and Functions.
When applied to Variables within a Function, it makes them retain their values between Function Calls.
When used with Global Variables or Functions, it restricts their scope to the current file.
Different ways to use the "static" keyword in C language
1 Static Variable within Functions
Static Variable When used inside a function, "static" helps the variable remember its value each time the function is called.
This indicates that the variable is set up just one time and keeps its value for the entire program.
2 Static Variable at File scope
When used outside of any function "static" keeps variables and functions only visible within the current file.
When you mark variables and functions as "static", they can't be accessed from other files using the "extern" keyword.
3 Static Functions
When you put "static" before a function, it means only the current file can use it.
Even if they access with "extern," other files can't enter the main() function because it's like an exclusive event inside the program.
4 Static Variable within structs
Using "static" with a member variable of a struct indicates that the variable is shared among all instances of that struct, like having a shared piece of information for all instances.
Every time you make a new struct, it doesn't have its own version of the variable. Instead, they all use the same one.
5 Static Global Variable
When "static" is applied to a member variable of a struct, it indicates that the variable is shared among all instances of that struct.
The variable cannot be accessed from other files using the "extern" static keyword.
Example:
#include<stdio.h>// Static global variable declaration static int staticGlobalVar = 10; // Function to modify static global variable void modifyStaticGlobal() { staticGlobalVar += 5; // Increment the static global variable by 5 } // Function to display static global variable void displayStaticGlobal() { printf("Static global variable value: %d\n", staticGlobalVar); } int main() { printf("Initial value of static global variable: %d\n", staticGlobalVar); // Modify static global variable modifyStaticGlobal(); printf("Value of static global variable after modification: %d\n", staticGlobalVar); // Display static global variable displayStaticGlobal(); return 0; }
Output:
Initial value of static global variable: 10 Value of static global variable after modification: 15 Static global variable value: 15
6 Static Local Variable
Regular local variables are like Temporary Containers. Each time a function is called, new containers are created, which start fresh and empty.
static local variables are more like reusable containers.
If you store something in them during the first run, they'll keep that information for the next time the function is used.
It's like having a cup you can use multiple times without throwing it away after each use.
Below is a simple example showcasing the "static" keyword's usage in C
#include<stdio.h> void count() { // static variable declaration static int counter = 0; // Increment the counter counter++; //print the counter value printf("counter:%d\n", counter); } int main(){ // call the function multiple times count(); count(); count(); return 0; }
Output:
counter:1 counter:2 counter:3
Share:
Comments
Waiting for your comments