
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Where are static variables stored in C/C++?
Static variables are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.
The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program.
All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment). Compared to this, the static variables that are initialized are stored in the initialized data segment.
An example of this is given as follows −
static int x = 5; static int y; The static variable x is stored in the initialized data segment and the static variable y is stored in the BSS segment.
A program that demonstrates static variables in C is given as follows −
Example
#include<stdio.h> int func(){ static int i = 4 ; i++; return i; } int main(){ printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); return 0; }
The output of the above program is as follows −
5 6 7 8 9 10
Now let us understand the above program.
In the function func(), i is a static variable that is initialized to 4. So it is stored in the initialized data segment. Then i is incremented and its value is returned. The code snippet that shows this is as follows −
int func(){ static int i = 4 ; i++; return i; }
In the function main(), the function func() is called 6 times and it returns the value of i which is printed. Since i is a static variable, it remains in the memory while the program is running and it provides consistent values. The code snippet that shows this is as follows −
printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func()); printf("%d\n", func());
- Related Articles
- Where are JavaScript variables stored?
- Where objects, methods and variables are stored in memory in Java?
- Static Variables in C
- Are the values of static variables stored when an object is serialized in Java?
- What are the local static variables in C language?
- Initialization of static variables in C
- Templates and Static variables in C++
- Class and Static Variables in C#
- Where and how to use static variables in Android using Kotlin?
- Where and how to use to static variables in android studio?
- Default values of static variables in C
- Are static local variables allowed in Java?\n
- Static variables in Java
- Initialization of global and static variables in C
- Why are global and static variables initialized to their default values in C/C++?
