Junior
What will happen if a global variable is declared with the static modifier?
sobes.tech AI
Answer from AI
The scope will be limited to the boundaries of the file in which it is declared. The variable will only be visible within this file.
The lifecycle remains the same: the variable will be created when the program starts and destroyed when it ends.
Example:
// file1.cpp
static int globalStaticVar = 10;
void func1() {
// Access to globalStaticVar is possible
globalStaticVar++;
}
// file2.cpp
extern int globalStaticVar; // Compilation error: globalStaticVar is not visible
void func2() {
// Access to globalStaticVar is impossible
}