The Wayback Machine - https://web.archive.org/web/20211031132411/https://www.geeksforgeeks.org/g-fact-16/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How are variables scoped in C – Static or Dynamic?

  • Difficulty Level : Easy
  • Last Updated : 12 Sep, 2018

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.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.




# 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();
}

References:
http://en.wikipedia.org/wiki/Scope_%28programming%29

My Personal Notes arrow_drop_up
Recommended Articles
Page :