Updated March 17, 2023
Introduction to Static Keyword in C
Static keyword in C varies differently in a way that it is a keyword that can be used with variables as well as with functions. Therefore, it is very much needed to get a demarcation on both to get the actual characteristics or the behavior of the keyword specifically in terms of C language. Its bit difficult to get the complete understanding so thoroughly for a beginner so we will drive through the syntax, actual working how it works in C, some norms and rules to be followed as it can be bit confusing to get when to make it use for scope of variable and when to use for scope of functional block.
Syntax
1. Syntax of static keyword in C when defining a variable:
static <variables's type> <variable's name>
<variable's type> static <variable's name>
Examples of syntax for static variables:
static int run = 0;
int static sleep = 0;
2. Syntax of static keyword in C when defining a function:
static <function's type> <function's name ()>
<function's type>static<function's name ()>/code>
Examples of syntax for static functions:
static void run ()
{
}
void static sleep ()
{
}
How Static Keyword works in C?
Basically, there are two ways in which static keyword works in terms of C.
- The static keyword inside a function.
- The static keyword outside a function.
1. Static keyword inside a function
Declaration of the variable within a function is associated with the compile-time and storage duration of the variable within a function call. I simple words, variables defined as static extends their scope for multiple function calls and once declared cannot lose its scope till the end of the program execution finishes. It starts to define its scope at compile time itself.
Example
# include<stdio.h>
int main ()
{
run ();
run ();
run ();
}
void run ()
{
static int running = 0;
++running;
printf ("The function \"run\" was called %d times.\n", running);
}
Output:
2. Static keyword outside a function
Once the static keyword is declared outside a function it limits its scope of the variable and becomes visible to the current file only which means the function will get its scope limited to its source file itself. Accessing of that static variable or function gets constraint from another source file.
Although it is considered good to declare a function as static which helps in data encapsulation to a specific scope. People from OOPs background or java background can easily relate it with the private and the public keyword functioning.
Example
To define variables in outside function i.e. at the global level we need to define at least static keyword.
Define one static variable in one source file and the same variable in the second variable in another source file.
Source1.c
static int sleep = 0;
Source2.c
static int sleep= 0;
static void sleep ()
{
static int sleeping = 0;
++ sleeping;
printf ("the function \ "sleep\" was called %d times.\n", sleeping);
}
Output:
Specifying the minimum size of an array parameter
There is one very essential use which we ignore sometimes let’s have a look into it.
We can tell the compiler the storage parameter for the minimum declaration of elements within an array pointer to a function as well. But this case is followed very rarely (followed till C99 compiler).
Example
Void print Array (int myArray [static 10], int size)
{
int i;
printf ("[");
for (i<0; i<size; ++i)
{
printf ("%d", myArray[i]);
}
printf ("] \n");
}
Basically, this will inform that the argument will be not null.
Rules and Regulations for the static keyword in C
1. In terms of Static Variable
- A static variable always remains alive when a program is in running state unlike auto and reserved keywords.
- Storage and memory allocation occur in the data segment, not in the stack segment.
- By default, if the variable is not declared with any value by default it will define explicitly by the compiler as 0.
- Declaration of static variables is mostly done using constant literals.
- It is primarily considered that we should not define a static variable inside a function it mostly makes the entire functional module a bit complex.
2. In terms of Static Function
- Declaration of the variable is basically considered as global by default, therefore using static keyword with a function makes it restricted within scope by limiting the scope within the same source file.
- One good characteristic of making the function static is reusability i.e. we can call the same function multiple times whenever required for execution.
Advantages
- Considering a variable as static is advantageous in the sense It helps in optimizing the entire code flow.
- Reusability and redefinition help the compiler to call any variable internally without making any other keyword like extern to dominate.
- One another advantage is increasing the readability in a way that the future will get an acknowledgment that the file is declared as static and is not accessible by any other file or source file.
- It also prevents a lot from declaring the c file i.e. redeclaring it as some other extern variable.
Conclusion
- The term “static” has its own trend whether to be considered as senseful or without sense. The different programming language has different meanings only related to the static keyword. Object-oriented language, it behaves with an encapsulation property very nicely.
- In terms of C, C#, and C++ It has a different nature. Some consider the usage of static as best because of the advantages it boasts of like optimization, reusability, scope limitation. Most of which is the acknowledgment of the final readable file.
- Later sometime including some naïve users can easily get to know if two source files don’t match by tallying the internal contents of the static functions and static variable as it will through the compilation error.
- But in terms of C, this is not considered a safe and secure way because the internal communication between threads become unsafe and difficult as it is ultimately becoming a global variable. Therefore sometimes it is considered not so conventional way in terms of C to make use of static keyword in C.
Recommended Articles
This is a guide to Static Keyword in C. Here we discuss How Static Keyword works in C with the Rules and Regulations. You may also look at the following article to learn more –