Static in C

What do you mean by Static in C programming?

Static is a keyword usable in C /C++ programming languages. It can be use with variables and functions.
An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is all over the C program.

Static in C
Static in C

Static keyword can be use in the following situations in C:

Static global variable:

When a global variable is declared with a keyword ‘ static ‘, then it is known as a static global variable. It is declared at the beginning of the program, and its visibility is all over the program.

Static function:

When a function declared with a keyword ‘ static ‘, then it known as a static function. Its lifetime is all over the program.

Static local variable:

When a local variable declared with a keyword ‘ static ‘, then it known as a static local variable. The memory of a static local variable is valid all over the program. But the scope of visibility of a variable is the same as the automatic local variables.

However, when the function modifies a static local variable during its first function call, this modified value will also be available for the next function call.

Static member variables:

When a member variable in a class declared with a static keyword, then known as a static member variable. It can be access by all the instances of a class.

Static method:

A member function of a class declared with a static keyword known as a static method. It can be access by all instances of a class.

Example:

Let’s understand through a simple example.

#include <stdio.h>
int main()
{
printf(" %d ",func());
printf(" n%d ",func());
return 0;
}
int func()
{
int num=0; // variable initialization
num++; // incrementing counter variable
return num;
}

In the above code, the func() function is called. In func(), num variable get updated. As soon as the function completes its execution, the memory of the num variable will be removed. If we do not want to remove the num from memory, then we need to use the num variable as static.
If we declare the variable static, the variable will not be remove from memory even after the function completes its execution.

Static variable:

A static variable is a variable that retains its value in various function calls.

Syntax:-

Here is the syntax of a static variable:

static data_type variable_name;
Example:

Let’s take a look at a simple example of static variables.

#include <stdio.h>
int main()
{
printf(" %d ",func());
printf(" n%d ",func());
return 0;
}
int func()
{
static int num=0;
num++;
return num;
}

Here the variable num declared as static. When the func() function called, the value of num variable updated to 1. During the next function call, the value of the num variable updates to 2.

Therefore, we can say that the value of the constant variable remains within the function call.

Static Function:

As we know non-static functions are global by default i.e., the function can also be access outside the file, but if we declare the function static, then it limits the function scope. The static function can only be access within a file.

The static function would look like as:

static void func()
{
printf(" Hello Doubtcrack members ");
}

Differences between the static & global variable

GLOBAL VARIABLE: Global variables are those variables which declared outside all functions. These global variables exist at the beginning of the program, and its scope remains until the end of the program. It can also be access outside the program.

STATIC VARIABLE: Static variables are limited to those source files in which they are defined, i.e., they are not accessible by the other source files.

NOTE:- Static & global variables both have static initialization. Here, static initialization means if we don’t assign any value to the variable then by default, 0 value will be assign to the variable.

Differences between static local & static global variable – C

Static global variable: If the variable declared with the static keyword outside the function, then known as the static global variable. It is accessible throughout the program.

Static local variable: The variable with the keyword ‘static’ declared inside a function, known as a static local variable.

The scope of the static local variable will be the same as the automatic local variable. But its memory will be available in the entire program execution. When the function modifies the value of a static local variable during a function call, it will remain the same during the next function call.

Properties of a static variable:

The following are the properties of a static variable:

  • The memory of a static variable is allocate within a static variable.
  • Its memory is available throughout the program, but the scope will remain the same as the automatic local variables. Its
  • value will persist across the various function calls.
  • If we do not assign any value to the variable, then the default value will be 0.
  • A global static variable cannot be access outside the program, while a global variable can be accessed by other source files.

Leave a Comment