Global vs Static vs Const Variable in C with Example
In C, variables can be declared with various storage classes and qualifiers, including global, static, and const. These determine the scope, lifetime, and mutability of variables. Let’s explore each one with examples.
1. Global Variable
A global variable is declared outside of any function and is accessible throughout the entire program.
Example:
#include <stdio.h>
// Declaration of a global variable
int global_variable = 42;
int main() {
// Accessing the global variable
printf("The value of global_variable is: %d\n", global_variable);
return 0;
}
In this example, global_variable
is accessible from any part of the program, providing a single point of data that can be shared across different functions.
2. Static Variable
A static variable has a scope limited to the block in which it is declared, and it retains its value between function calls.
Example:
#include <stdio.h>
void example_function() {
// Declaration of a static variable
static int static_variable = 0;
// Accessing and updating the static variable
printf("The value of static_variable is: %d\n", static_variable);
static_variable++;
}
int main() {
// Calling the function multiple times
example_function();
example_function();
example_function();
return 0;
}
In this example, static_variable
retains its value between calls to example_function
, incrementing each time the function is called.
3. Const Variable
A const
variable is one whose value cannot be modified during the program’s execution.
Example:
#include <stdio.h>
int main() {
// Declaration of a const variable
const int const_variable = 42;
// Accessing the const variable
printf("The value of const_variable is: %d\n", const_variable);
// Uncommenting the next line would result in a compilation error
// const_variable = 50; // Error: assignment of read-only variable 'const_variable'
return 0;
}
In this example, const_variable
is declared as a constant. Attempting to modify its value results in a compilation error, enforcing immutability.
Combining Global, Static, and Const
You can combine these qualifiers to create variables with specific characteristics, such as a global static const variable.
Example:
#include <stdio.h>
// Declaration of a global static const variable
static const int GLOBAL_STATIC_CONST = 42;
int main() {
// Accessing the global static const variable
printf("The value of GLOBAL_STATIC_CONST is: %d\n", GLOBAL_STATIC_CONST);
// Uncommenting the next line would result in a compilation error
// GLOBAL_STATIC_CONST = 50; // Error: assignment of read-only variable 'GLOBAL_STATIC_CONST'
return 0;
}
In this example, GLOBAL_STATIC_CONST
is a global static const variable, combining:
- Global: Accessible throughout the program.
- Static: Limited to the translation unit (the file where it is defined).
- Const: Its value cannot be modified.
Summary
- Global Variables: Accessible throughout the entire program.
- Static Variables: Retain their value between function calls and have a limited scope.
- Const Variables: Their values cannot be modified after initialization.
- Combining Qualifiers: Provides granular control over a variable’s scope, lifetime, and immutability.
Understanding these storage classes and qualifiers allows for more precise control over variable behavior, enhancing the maintainability and efficiency of your C programs.
# Written by Elliyas Ahmed