How are variables scoped in C

C++Server Side ProgrammingProgramming

Here we will see how the C variables are scoped. The variables are always statically scoped in C. Binding of a variable, can be determined by the program text. These are independent of runtime function call stack.

Let us see one example to get the idea.


# include <stdio.h>
int x = 0;
int my_function() {
   return x;
int my_function2() {
   int x = 1;
   return my_function();
int main(){
   printf("The value is: %d\n", my_function2());


The value is: 0

Here the result is 0. Because the value returned by my_function() is not depends on the function, which is calling this. This function always returns the value of the global variable x.

Published on 30-Apr-2019 12:40:14