- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C static Variables
![]() Share with a Friend |
C Programming - C static Variables
C Static Variables
In C, static variables are variables that maintain their values between function calls. The key difference between static and regular (automatic) variables is that static variables retain their value even after the function in which they are defined has finished executing. Static variables are particularly useful when you need a variable to preserve its state across multiple calls to the same function.
Key Characteristics of Static Variables:
- Lifetime: A static variable persists throughout the lifetime of the program, retaining its value between function calls.
- Scope: If declared inside a function, a static variable has local scope but a global lifetime. If declared outside any function, it has file scope and can only be accessed within the file.
- Initialization: A static variable is initialized only once, and its value persists between function calls. If not explicitly initialized, it is automatically initialized to zero.
Syntax of Static Variables
- Static Variable Inside a Function: When a static variable is declared inside a function, it behaves like a local variable (only visible within the function), but it does not lose its value when the function exits. The value is preserved between subsequent calls to the function.
C
static int count = 0; // Static variable initialization
- Static Variable Outside a Function: When a static variable is declared outside of any function, it is accessible only within the file. It cannot be accessed by functions in other files, even though it has a global lifetime.
C
static int count = 0; // Static variable with file scope
Example: Static Variable Inside a Function
C
#include <stdio.h>
void incrementCounter() {
static int count = 0; // Static variable
count++;
printf("Count: %d\n", count);
}
int main() {
incrementCounter(); // Output: Count: 1
incrementCounter(); // Output: Count: 2
incrementCounter(); // Output: Count: 3
return 0;
}
Explanation:
- The count variable is declared as static inside the incrementCounter() function. Even though it is local to the function, it retains its value across multiple calls.
- The value of count starts at 0 and is incremented each time the function is called. Unlike regular local variables, the value is not reset to 0 on each call.
Example: Static Variable with File Scope
C
#include <stdio.h>
static int globalCount = 100; // Static variable with file scope
void displayCount() {
printf("Global Count: %d\n", globalCount);
}
int main() {
displayCount(); // Output: Global Count: 100
globalCount += 10;
displayCount(); // Output: Global Count: 110
return 0;
}
Explanation:
- globalCount is a static variable with file scope. This means it is accessible only within the file where it is declared.
- The variable retains its value across different functions within the file and persists throughout the program's execution.
- The value of globalCount is updated in the main() function, and the updated value is reflected when displayCount() is called again.
Why Use Static Variables?
- State Preservation: Static variables are useful when you need to preserve state across function calls, such as counting the number of function calls or storing data that should not be reset.
- Encapsulation: When static variables are declared inside a function, they help encapsulate the variable's value, ensuring that it cannot be accessed or modified from outside the function, but still retains its state across calls.
- File-Scope Variables: Static variables can be used when you want a global variable to be accessible only within the current file, thus preventing unwanted modifications or access from other files.
Static Variables and Memory Allocation
- Static variables are stored in a special memory area called the data segment, unlike local variables which are stored on the stack.
- Since static variables retain their values throughout the program's execution, they are initialized only once and do not get deallocated after the function ends.
Summary of Static Variables in C
- Scope:
- Inside a function: Local scope, but retains its value across calls.
- Outside a function: File scope, accessible only within the file.
- Lifetime: Static variables exist for the entire duration of the program and retain their values between function calls.
- Initialization: Static variables are initialized only once. If not initialized explicitly, they are automatically initialized to zero.
- Usage: They are used when you need to preserve the state of a variable between function calls, or when you need global variables that are not accessible outside the file.
Static variables are an important feature in C, particularly for managing persistent state or ensuring data encapsulation within functions.
