Static in C Language

In the C programming language, the keyword "static" is used to modify the behavior of variables and functions. It has different meanings depending on where it is used. In this tutorial, we will explore the usage of the "static" keyword with variables and functions, and understand its significance in C programming.

Properties of Static Variables

1) Storage Duration:

When a variable is declared as static within a function, it changes the storage duration of the variable. Normally, variables inside a function have automatic storage duration, which means they are created when the function is called and destroyed when it returns. However, static variables have a different storage duration. They are allocated memory once when the program starts and retain their values between function calls.

2) Lifetime:

Static variables have a lifetime that extends throughout the entire program's execution. They are not destroyed when a function ends like automatic variables. Instead, they retain their values and remain in memory until the program terminates.

3) Initialization:

Static variables are automatically initialized to zero by default. If a specific value is assigned during declaration, the variable will be initialized with that value. Subsequent function calls will retain the updated value from the previous call.

4) Scope:

Static variables have a scope limited to the block or function where they are defined. They are not accessible outside of their enclosing block or function. However, static variables declared outside of any function (at the global level) have a file scope, making them accessible within the entire file.

5) Visibility:

Static variables are not visible to other functions or blocks in the same file. This provides encapsulation and prevents name clashes with variables in other functions or files.

5) Memory Allocation:

Static variables are allocated memory once when the program starts, regardless of whether they are inside a function or at the global level. The allocated memory remains fixed throughout the program's execution.

Static Variable

When the "static" keyword is used with a variable inside a function, it changes the variable's storage duration. Normally, variables declared inside a function have automatic storage duration, meaning they are created when the function is called and destroyed when the function ends. However, when a variable is declared as static, it retains its value between different function calls.

Here's an example to illustrate the concept:

In this example, the "count" variable is declared as static inside the "increment" function. As a result, it retains its value between multiple function calls. The output shows that the value of "count" keeps incrementing each time the function is called.

Static Functions

The "static" keyword can also be used with functions in C. When a function is declared as static, it restricts the scope of the function to the current source file. It means that the function cannot be accessed or called from other files using the extern keyword.

Here's an example to demonstrate the usage of static functions:

In this example, the function "printMessage" is declared as static. It can only be called from within the same source file where it is defined. If you try to call this function from another file using extern, it will result in a compilation error.

Differences Between Static and Global Variables:

AspectStatic VariableGlobal Variable
ScopeLimited to the block or function where definedAvailable to the entire program
LifetimeRetains value between function callsRetains value throughout the program
AccessibilityLimited to the block or function where definedAccessible from any part of the program
Memory AllocationAllocated once and shared among all instancesAllocated once and shared among all files
InitializationAutomatically initialized to zeroAutomatically initialized to zero
Conflicts and CollisionsNo conflicts with variables of the same nameCan lead to naming conflicts in a program

Example:

Let's consider an example to understand the differences between static and global variables:

In this example, we have a static variable, staticVar, declared inside the testFunction(). This variable retains its value between function calls and is not accessible from outside the function. We also have a global variable, globalVar, declared outside any function, making it accessible throughout the program. The local variable, localVar, is only accessible within the testFunction().