- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Static Variables in C
By default, a C variable is classified as an auto storage type. A static variable is useful when you want to preserve a certain value between calls to different functions. Static variables are also used to store data that should be shared between multiple functions.
Static Variables
The static variables belong to the static storage class, they are initialized only once and preserve the values till the end of the program, The static keyword is used to declare the static variables.
Features of Static Variables
The following are some of the features of static variables in C programming language −
- The compiler allocates space to a static variable in the computer’s main memory.
- Unlike auto, a static variable is initialized to zero and not garbage.
- A static variable is not re-initialized on every function call, if it is declared inside a function.
- A static variable has local scope.
Declare a Static Variable
To declare a static variable in C language, use the static keyword and assign the initial value. Following is the syntax to declare a static variable:
static datatype var = value;
Here,
- datatype represents the type of variable like int, char, float, etc.
- var is the name of variable given by user.
- value is any value given to initialize the variable. By default, it is zero.
Examples of Static Variables in C
Example: Using Static Variable
Here is an example of how you can use a static variable in C language −
#include <stdio.h> int main(){ auto int a = -28; static int b = 8; printf("The value of auto variable: %d\n", a); printf("The value of static variable b: %d\n",b); if(a != 0) printf("The sum of static variable and auto variable: %d\n",(b+a)); return 0; }
Output
When you run this code, it will produce the following output −
The value of auto variable: -28 The value of static variable b: 8 The sum of static variable and auto variable: -20
Example: Create Counter Function W/O Using Static Variable
In this example, x is an auto variable by default and initialized to 0 every time when the counter() function is called. On each subsequent call, it gets re-initialized.
#include <stdio.h> int counter(); int main(){ counter(); counter(); counter(); return 0; } int counter(){ int x; printf("Value of x as it enters the function: %d\n", x); x++; printf("Incremented value of x: %d\n", x); }
Output
Run the code and check its output −
Value of x as it enters the function: 0 Incremented value of x: 1 Value of x as it enters the function: 0 Incremented value of x: 1 Value of x as it enters the function: 0 Incremented value of x: 1
However, when the variable x in the counter() function is declared as static, it is initialized to "0" when the counter() function is called for the first time. On each subsequent call, it is not re-initialized. Instead, it retains the earlier value.
Example: Create Counter Using Static Variable
Change the declaration of "x" to "static int x = 0;" and run the program again −
#include <stdio.h> int counter(); int main(){ counter(); counter(); counter(); return 0; } int counter(){ static int x = 0; printf("Value of x as it enters the function: %d\n", x); x++; printf("Incremented value of x: %d\n", x); }
Output
Now, when you run this code, it will produce the following output −
Value of x as it enters the function: 0 Incremented value of x: 1 Value of x as it enters the function: 1 Incremented value of x: 2 Value of x as it enters the function: 2 Incremented value of x: 3
Passing Static Variable to Function
You can pass a static variable to a function. However, a formal parameter cannot be declared as static, as C uses the function parameters as local auto variables inside a function.
Example
In this code, we pass a static variable to a function. However, the change in its value is not reflected in the calling function.
#include <stdio.h> int myfunction(int x); int main(){ static int x = 5; myfunction(x); printf("in main - x:%d\n", x); return 0; } int myfunction(int x){ x++; printf("Incremented value of x: %d\n", x); }
Output
Run the coce and check its output −
Incremented value of x: 6 in main - x:5
Similarities Between Static and Global Variables
A static variable has certain similarities with a global variable. Both of them, if not explicitly initialized, both are initialized to "0" (for numeric types) or "null pointers" (for pointers).
The scope of a static variable is restricted to the function or the block in which it is declared. This is unlike a global variable, which is accessible throughout the program. Also, a static variable can be imported in another code file, as we do by using the extern keyword.
Example
You can declare a global variable as static too. Take a look at the following example −
#include <stdio.h> int myfunction(); static int x = 5; int main(){ myfunction(x); printf("Inside the main function, x: %d\n", x); return 0; } int myfunction(){ x++; printf("Incremented value of x: %d\n", x); }
Output
When you run this code, it will produce the following output −
Incremented value of x: 6 Inside the main function, x: 6
It is better to use static variables to be accessible only within a file. On the other hand, use global (with extern) variables to be accessible from anywhere in a program (if declared extern in other files).