Static Identifier in C
Static variables or identifiers in C are variables that have been defined using the static keyword and are used to change the variable's scope and storing length.
One advantage of static variables is that their value persists regardless of when they are removed from their range! As a result, a static variable does not require initialization in its fresh scope; instead, it retains its existing value from its prior content.
Information about C's static Identifier:
1) During the operation of the code, a static int constant is kept in memory. Once an algorithm call that created an ordinary or automated variable ends, that variable is deleted.
2) An auto variable cannot be utilized for recording the total amount of instances the function is called; however, we may employ static int to do so.
Example Code:
#include <stdio.h>
int function()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", function());
printf("%d ", function());
printf("%d",function());
return 0;
}
Output:
Since static variables can only be created once and remain valid until the program's conclusion, the program mentioned before prints 1 and 2 can hold onto their significance in between function calls because of this.
Example Code:
The same code is for the local auto variable.
#include <stdio.h>
int function()
{
int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", function());
printf("%d ", function());
printf("%d ", function());
printf("%d ", function());
return 0;
}
Output:
2) Memory allotment for static variables occurs in the information segment rather than the stack section. For more information, see the memory arrangement of programs written in C.
3) If a static variable isn't expressly set up, it is initialized to 0, just as global variables are. For instance, the y quantity is displayed as garbage in the application below, but the x variable appears on paper as 0.
Example Code:
#include <stdio.h>
int main()
{
static int a;
int b;
printf("%d \n%d", a, b);
}
Output:
4) The only way to set up static variables in C is to use constant literals. For example, the creation of the following program fails.
Example Code:
#include<stdio.h>
int initializer(void)
{
return 20;
}
int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}
Output:
When associated with a variable, the keyword static is used in two primary contexts: inside procedures and on the global level.
1. Static Variables inside Functions:
The memory period of a variable is changed from spontaneous (the default for local variables) to static whenever the static keyword is employed inside a function. This occurs before a variable definition. This indicates that the variable's value is maintained throughout the function calls.
Example Code:
#include <stdio.h>
void exampleFun() {
static int count = 0;
count++;
printf("Count: %d\n", count);
}
int main() {
example();
example();
return 0;
}
Output:
2. Static Variable at the Global Level:
The keyword static limits the variable's accessibility to the present-day source file by providing internal linking when the variable is employed at a global scale (within any function). This indicates that the value can only be found in that parent file.
3. Static Functions:
The static keyword restricts a function's scope to the document in which it exists when employed preceding a function definition. The process is now "statically" localized to the file.
4. Static Components in Structs:
Use the static keyword to designate an element as a member that remains constant within a struct. This indicates that instead of having a single instance of that property for each use of the struct, there is a single instance for the whole structure.
5. File-Specific Global Variables:
When the source file uses silent at the level of the entire world, a global variable unique to that file is created. Encapsulating information in just one file and avoiding worldwide namespace contamination can benefit both.
Properties of Static Variables:
- Initialization: Static variables are immediately set to 0 if no starting point is supplied.
- Lifetime: The lifespan of a static variable is the duration of the program's execution.
- Scope: Based on whether a static variable is global or local, its application is restricted to the section of code or file during which it is defined.
- Visibility: Just the file in which they are defined contains information about global variables that are static.
- Application: When you wish to restrict the application of a global variable to a particular source document or when you require the variable to hold its value during multiple function calls, static variables come in handy.