Table of contents
1.
Introduction
2.
What is static keyword in C?
2.1.
Syntax
3.
Static Variables in C
4.
How to Declare and Initialize Static Keyword in C?
4.1.
C
5.
Static Keywords in C examples
5.1.
Static Variables
5.2.
C
5.3.
Static Functions
6.
Frequently Asked Questions
6.1.
What are the differences between static and global variables?
6.2.
What are the differences between a static local and static global variable?
6.3.
Why should static variables not be declared inside the structure?
6.4.
What are the advantages of static variables?
7.
Conclusion
Last Updated: Dec 3, 2024
Easy

Static Keyword in C

Author Tanay kumar Deo
5 upvotes

Introduction

We can use a Static keyword in the C Programming language with both functions and variables, i.e., we may declare a static function and a static variable. A standard variable is limited to its scope, while the scope of a static variable is throughout the program. The static keyword in C has the property of preserving its value even after it is out of scope. 

Static Keyword in C

In this article, we will focus on the static keyword in C. We will discuss scenarios to use static keywords, with examples of static variables and static functions.

What is static keyword in C?

The static keyword in C is a storage class specifier. It has different meanings based on the context. Within a function, it makes the variable retain its value among multiple function calls. Outside of a function, it restrains the visibility of the variable or function to the current file. 

We can use static keywords in C for the following scenarios−

  • Static function: If the function is declared with the static keyword, it is known as a static function. Its lifetime is through the complete program.
  • Static global variable: When we declare any global variable with a static keyword, it is known as a static global variable. Generally, we declare a static global variable at the top of the program, and its visibility is throughout the program.
  • Static local variable: When a lthe ocal variable is declared with a static keyword, it is known as a static local variable.  Local variables have the scope that, they are visible only within the block or function like local variables have the scope that, they are visible only within the block or function like local variables.
  • Static method: If we declare the member function of a class with a static keyword in C, it is known as a static method. We can access it through all the instances of the class.
  • Static member variables: If we declare the member variables of a class with a static keyword in C, it is known as a static method. We can access it through all the instances of the class.

Must Read Static Blocks In Java

Syntax

The syntax for a static keyword in C is given below:

// Static Keyword in C for variables
static <variable_type> <variable_name>
<variable_type> static <variable_name>


// Static Keyword in C for functions
static <function_type> <function_name>()
<function_type> static <function_name>()

 

You can also read about the jump statement 

Static Variables in C

In C programming, a static variable is a variable that retains its value between function calls. It is initialized only once and remains in memory for the duration of the program's execution. Static variables are useful for maintaining state across multiple function calls within the same file or module.

How to Declare and Initialize Static Keyword in C?

To declare and initialize a static variable in C, you use the static keyword before the variable declaration. The static variable is initialized only once, at the start of the program execution, and retains its value throughout the program's lifespan.

Example:

  • C

C

#include <stdio.h>

void increment() {
// Static variable declaration and initialization
static int count = 0;
count++;
printf("Count: %d\n", count);
}

int main() {
increment();
increment();
increment();
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Count: 1
Count: 2
Count: 3

In this example, the static variable count is initialized to 0 when the increment() function is first called. Subsequent calls to increment() increment the value of count without reinitializing it, demonstrating the persistence of static variables across function calls.

Static Keywords in C examples

This section will see the examples of using a static keyword in C in different scenarios.

Static Variables

Let's have look at a simple example of static variables.

  • C

C

#include<stdio.h>
int fun(){
 static int count = 0;
 count++;
 return count;
}

int main(){
 printf("%d ", fun());
 printf("%d ", fun());
 printf("%d ", fun());
 return 0;
}
You can also try this code with Online C Compiler
Run Code

In the above example, we have initialized a static variable "count" with an initial value of 0. When we call the fun() function for the first time, the variable is created in the memory, which can only be destroyed once the complete program is completely executed. The output for the above code is shown below.

Output

Properties of Static Keyword:

The properties of a Static Variable in C programming language are as follows:

  1. The memory of the static variable is allocated within the static variable.
  2. Its memory is available through the complete program, but its scope will remain the same as the general local variables.
  3. Its value persists across various function calls.
  4. The default value for a static variable will be 0.

Static Functions

A non-static function is global by default, i.e., we can access that function outside the source file, but a static function limits the function scope. We can access the static function within a file only. An example of declaring a static function in C is shown below.

static int fun(){
  int count = 0;
  count++;
  return count;
}


Also see, Short int in C Programming

Frequently Asked Questions

What are the differences between static and global variables?

A global variable is a variable that is declared outside the functions. It exists at the beginning of the program, and its scope remains throughout the program. We can access it outside the program also.
While a static variable is limited to the source file in which it is defined, i.e., it is not accessible by the other source files. 

What are the differences between a static local and static global variable?

If a variable is declared outside the function with a static keyword, it is known as a static global variable. It is accessible through the complete program.
A static local variable is declared inside a function. The scope of a static local variable is the same as a normal local variable, but its memory is available throughout the program.

Why should static variables not be declared inside the structure?

Structure members should reside in the same memory segment because the value for the structure element is fetched by counting the offset of the element from the beginning address of the structure. Separating out one member alone to a data segment defeats the purpose of static variable and it is possible to have an entire structure as static.

What are the advantages of static variables?

Static Variable has the property of preserving their value even after they are out of scope. A static variable can preserve its value in its previous scope and can be used again without being initialized again.

Conclusion

In this article, we have extensively discussed static keyword in C with different examples. The static keyword in C is a powerful tool for managing variables and functions within a program. It allows variables to retain their values between function calls, ensuring data persistence and enabling efficient memory usage. 

Recommended reads: Bit stuffing program in c

Live masterclass