- 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 keyword
![]() Share with a Friend |
C Programming - C static keyword
The static Keyword in C
In C, the static keyword has several different uses, depending on where it is applied. It can be used with variables and functions to modify their behavior in terms of scope, lifetime, and visibility.
- Static Variables:
Inside Functions (Local Static Variables)
When static is used with a local variable inside a function, it changes the variable's lifetime. Normally, local variables are created when a function is called and destroyed when the function ends. However, a static variable inside a function retains its value between function calls. It is initialized only once, and its value is preserved throughout the program's execution.
Syntax:
C
static data_type variable_name;
- Lifetime: The variable persists for the lifetime of the program, but its scope remains within the function.
- Initialization: A static variable is initialized only once, and its value is retained across function calls.
Example:
C
#include <stdio.h>
void countCalls() {
static int count = 0; // static variable
count++;
printf("Function called %d times\n", count);
}
int main() {
countCalls(); // Output: Function called 1 times
countCalls(); // Output: Function called 2 times
countCalls(); // Output: Function called 3 times
return 0;
}
In this example, the variable count keeps its value between function calls, so it increments each time the function is called.
Outside Functions (Global Static Variables)
When static is used with a global variable (outside any function), it restricts the variable’s scope to the file in which it is declared. This means that the variable cannot be accessed or modified by functions in other files, even if they are part of the same program.
Syntax:
C
static data_type variable_name;
- Scope: The variable is limited to the file in which it is defined, so it is not visible to other files.
Example:
C
// File1.c
#include <stdio.h>
static int x = 10; // static variable
void printX() {
printf("x = %d\n", x);
}
// File2.c
#include <stdio.h>
// extern int x; // Error: 'x' is not visible outside File1.c
extern void printX();
int main() {
printX(); // Output: x = 10
return 0;
}
Here, x is declared as static in File1.c, so it cannot be accessed from File2.c.
- Static Functions:
The static keyword can also be applied to functions, limiting their scope to the file in which they are defined. This means the function can only be called within the same file and cannot be used by other files in the program.
Syntax:
C
static return_type function_name(parameters);
- Scope: The function is restricted to the file in which it is defined, so it cannot be used in other files, even if they are linked together.
Example:
C
// File1.c
#include <stdio.h>
static void greet() {
printf("Hello from static function!\n");
}
int main() {
greet(); // Output: Hello from static function!
return 0;
}
Here, the greet function is static, so it cannot be called from other files, even though they are part of the same project.
- Static Keyword and Memory Allocation:
For local static variables, memory is allocated only once, and it is initialized once, unlike regular local variables that are re-initialized every time the function is called. Static variables are typically stored in the data segment (not the stack), which persists for the entire program execution.
Advantages of Using the static Keyword:
- Persistence of local variables: Static local variables retain their value across multiple function calls, making them useful for counting or accumulating values.
- File-scope for global variables and functions: By using static for global variables and functions, you can avoid potential naming conflicts between different source files in large projects.
- Memory efficiency: Since static variables are initialized only once and retain their value throughout the program, they may save memory by not requiring repeated allocations.
Example Using Both Static Variables and Functions:
C
// File1.c
#include <stdio.h>
static int count = 0; // Static global variable
void incrementCount() {
count++;
printf("Count: %d\n", count);
}
static void showMessage() {
printf("This function is static!\n");
}
int main() {
showMessage(); // Can be called because it's within the same file
incrementCount(); // Count: 1
incrementCount(); // Count: 2
return 0;
}
In this example:
- count is a static global variable, so its value is retained across function calls.
- showMessage is a static function, so it is only visible within File1.c.
Summary:
- Local Static Variables: Retain their value across function calls and are initialized only once.
- Global Static Variables: Are confined to the file in which they are defined and cannot be accessed from other files.
- Static Functions: Are restricted to the file in which they are declared.
- Lifetime of Static Variables: For both local and global static variables, the lifetime is the entire program execution.
The static keyword is a useful tool in C programming for controlling the scope, lifetime, and visibility of variables and functions.
