Static Variables in C 

Static Variables in C 

5 mins read1.5K Views Comment
Updated on Aug 29, 2023 11:11 IST

In this article, you will learn about Static Variables in C programming. We will discuss the introduction, primary uses, and difference between static and global variables. You will also learn about the properties and syntax of static variables and how they can be used to maintain values across multiple function calls and preserve data between function executions.

2023_02_MicrosoftTeams-image-5.jpg

A static variable is a variable that is declared once and not destroyed until the program completes execution. It has the property of retaining its previous scope value when declared programmatically. They differ from regular variables because they do not retain their previous values. Regular variables are destroyed as soon as they go out of scope. However, when static variables are initialized, they are destroyed only after the entire program has run.

In this article, we will learn about C Programming.

Table of contents

So, without further ado, let’s get started!  

Introduction to Static Variables in C 

Static variables in C retain their value even after the function or block in which they were declared been executed. Unlike normal variables, which lose their value once they go out of scope, static variables maintain their value throughout the entire execution of a program. They are destroyed only after the program has been completed.  

They are declared using the “static” keyword before the variable declaration. Static variables are useful for maintaining a value across multiple function calls or preserving data between function executions. It’s important to have a good understanding of the following concepts before working with static variables

  • Variable Declaration 
  • Memory Allocation in C  

Syntax 

The syntax for declaring a static variable in C is shown below: 

 
static data_type variable_name;
Copy code

Where, 

  • data_type is the type of data you want to store (e.g., int, char, float, etc.) 
  • variable_name is the name of the static variable. 

Here’s an example of how to use malloc() to dynamically allocate memory for an array of integers in C: 

 
#include <stdio.h>
void func() {
static int count = 0;
count++;
printf("Count is %d\n", count);
}
int main() {
for (int i = 0; i < 5; i++) {
func();
}
return 0;
}
Copy code
Difference between Malloc and Calloc
Difference between Malloc and Calloc
Malloc and Calloc are dynamic memory allocation methods in C language. You will learn the differences between these two. And will also see the applications of them. In this article...read more
Stored procedure Vs. Function: What are the differences?
Stored procedure Vs. Function: What are the differences?
The stored procedure takes no parameter, can modify database objects, and need not return results; in contrast, the function can have input parameters and can be called from procedures. In...read more
Stored procedure Vs. Function: What are the differences?
Stored procedure Vs. Function: What are the differences?
The stored procedure takes no parameter, can modify database objects, and need not return results; in contrast, the function can have input parameters and can be called from procedures. In...read more

Primary Uses of the Static Keyword in C 

The primary uses of the “static” keyword in C programming are: 

  • To declare static variables: The “static” keyword can be used to declare static variables, which retain their value between function calls and have a limited scope. 
  • To create internal linkage: The “static” keyword can be used to create internal linkage for functions and variables, which means that the functions and variables are only accessible within the same file in which they were declared. 
  • To preserve data between function calls: The “static” keyword can be used to preserve data between function calls by declaring static variables within a function. 
  • To initialize a variable only once: The “static” keyword can be used to initialize a variable only once, as the value of a static variable is retained between function calls. 
  • To increase the scope of a variable: The “static” keyword can be used to increase the scope of a variable from local to file scope, making it accessible from any part of the code within the same file. 

In general, the “static” keyword controls the scope and lifetime of variables and functions in C programming and preserves data between function calls. 

Difference Between Static and Global Variables 

Static and global variables are both types of variables in C programming, but they differ in scope and lifetime. 

  • A global variable is a variable that is declared outside of all functions and is accessible from any part of the program. It has a global scope, meaning its value can be accessed and modified anywhere in the code. The lifetime of a global variable starts when the program is loaded into memory and lasts until the program exits. 
  • On the other hand, a static variable is a variable that is declared with the “static” keyword within a function or block. It has a local scope, meaning it can only be accessed within the function or block in which it was declared. The lifetime of a static variable starts when the function is first called and lasts until the end of the program. Unlike global variables, the value of a static variable is preserved across multiple function calls and is not destroyed when the function or block in which it was declared exits. 

Here’s a table summarizing the differences between static and global variables in C: 

Feature  Static Variables  Global Variables 
Scope  Local  Global 
Lifetime  Starts when function is first called, lasts until end of program  Starts when program is loaded into memory, lasts until program exits 
Value Preservation  Retains value between function calls  Does not retain value between function calls 
Accessibility  Only accessible within the function or block in which it was declared  Accessible from any part of the code 

Properties of Static Variables 

Static variables in C have the following properties: 

  • Retention of value: Static variables retain their value even after the function or block in which they were declared has been executed. This means that their value is preserved between function calls. 
  • Limited scope: Static variables are limited and can only be accessed within the function or block in which they were declared. 
  • Declaration: Static variables are declared using the “static” keyword in front of the variable declaration. 
  • Initialization: Static variables can be initialized when they are declared. If a static variable is not initialized, it will be set to zero (0) by default. 
  • Lifetime: The lifetime of a static variable starts when the function is first called and lasts until the end of the program. Unlike normal variables, which are destroyed when they go out of scope, static variables are destroyed only after the program has completed its execution. 
  • Memory allocation: Static variables are stored in the memory data segment, which is separate from the stack and heap. 

These properties make static variables useful for maintaining a value across multiple function calls or preserving data between function executions. 

Understanding the Difference Between Structure and Union in C
Understanding the Difference Between Structure and Union in C
A structure is a custom data type that holds multiple members of different data type under a single unit where union is a user defined data type that combine object...read more
Leap Year Programs in C 
Leap Year Programs in C 
This article will demonstrate Leap Year Programs using If Else Statement, While Loop, Conditional Operator, Switch Statement Using Functions.
File Handling in C
File Handling in C
This article deals with File Handling in C and will answer your query regarding is its usage.This concept is cleared with programming example in C.

Endnotes 

This article was helpful for you in understanding static variables in C Programming. The primary use of static variables is to maintain a value across multiple function calls or to preserve data between function executions. 

Explore our C articles to learn more about the language and consolidate your knowledge of the fundamentals. 

Contributed by-Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio