C Scope & Lifetime
In this page:
type global_name = value; // global scope
void function_name() {
type local_name = value; // local scope, exists only in this function
}
Local Scope
A variable declared inside a function has local scope, meaning it's only visible to code within that same function -- code in other functions has no way to reference it, even by the same name.
Example: Local Scope
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
// Declare local and set it to 5
int local = 5;
// Print formatted text to the screen
printf("%d", local);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Global Scope
A variable declared outside of every function has global scope, making it visible to and modifiable by any function in the program -- convenient for shared state, but riskier since any function could change its value unexpectedly.
Example: Global Scope
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare counter and set it to 10
int counter = 10;
void show() {
// Print formatted text to the screen
printf("%d", counter);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Block Scope
Variables declared inside an if block or loop have block scope, meaning they only exist and are only accessible within that specific pair of curly braces, disappearing once execution leaves that block.
Example: Block Scope
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Check whether 1
if (1) {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d", x);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Lifetime
A variable's lifetime is how long its memory allocation persists during the program's execution -- a local variable's memory is reclaimed automatically the moment its enclosing function returns, and its old value becomes inaccessible.
Example: Variable Lifetime
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d", x);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Name Hiding
If a local variable shares a name with a global variable, C lets the local one take precedence inside its own scope, effectively hiding the global's value there -- the global is unaffected and still accessible from other functions.
Example: Variable Name Hiding
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare value and set it to 100
int value = 100;
void show() {
// Declare value and set it to 5
int value = 5;
// Print formatted text to the screen
printf("%d", value);
}
// Program execution starts in main()
int main() {
show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using a local variable outside the block where it was declared.
- Shadowing a global with a local of the same name and changing the wrong one.
- Expecting a local variable to keep its value between calls, when it is reset unless declared
static.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: