Open In App
Related Articles

How are variables scoped in C – Static or Dynamic?

Improve Article
Improve
Save Article
Save
Like Article
Like

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 the global variable x. 

C




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


Output

0

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Similar Reads