The Scope, Visibility, and Lifetime of Variables in C
Scope of a Variable: The scope of a variable in C refers to the region of the program where the variable can be accessed. It defines where the variable is valid and can be used.
Visibility of a Variable: Visibility determines whether a variable is accessible or hidden from other parts of the program. Variables can have local or global visibility based on their declaration.
Lifetime of a Variable: The lifetime of a variable is the duration during which the variable exists in memory. It starts when the variable is declared and ends when it goes out of scope or the program terminates.
Syntax:
Local Variable:
void functionName() {
int localVar; // local variable
// code
}
Global Variable:
int globalVar; // global variable
void main() {
// code
}
Static Variable:
void functionName() {
static int staticVar; // static variable
// code
}
Program.c
Output:
Global Variable in main: 10
Local Variable: 5
Global Variable: 10
Static Variable: 20
Resources :
Featured
Special title treatment
With supporting text below as a natural lead-in to additional content.