How are variables scoped in C – Static or Dynamic?
In C, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack.
For example, output for the below program is 0, i.e., the value returned by f() is not dependent on who is calling it. f() always returns the value of global variable x.
# include <stdio.h> int x = 0; int f() { return x; } int g() { int x = 1; return f(); } int main() { printf("%d", g()); printf("\n"); getchar(); } |
chevron_right
filter_none
References:
http://en.wikipedia.org/wiki/Scope_%28programming%29
Recommended Posts:
- Static Variables in C
- Initialization of static variables in C
- Initialization of global and static variables in C
- What are the default values of static variables in C?
- Static functions in C
- Static data members in C++
- C++ | Static Keyword | Question 6
- C++ | Static Keyword | Question 5
- C++ | Static Keyword | Question 4
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 1
- Can static functions be virtual in C++?
- When are static objects destroyed?
- Difference between Static and Shared libraries
Improved By : InathiSirayi



