C Variables Scope

The Scope of a variable can be defined as the range of availability of a variable to the program in which it is declared. C has two different variables scopes :

Local scope :
Global scope :

1. Local scope :

A variable that is visible only in a specific context - for example a function, has a local scope. A local variable will be visible only within a function where it is defined. In simple words a variable declared within a function has a local scope and can only be referenced within that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function. For example, take a look at below code :
#include <stdio.h>

void MyFunc() {
  int x = 10;
  printf("%d\n", x);

int main() {
Output :


At the above example the variable x inside the MyFunc() is local variable to the function.

2. Global variable :

A variable that is declared outside the function or block is called global variable. Any function can change the value of the global variable. It is available to all the functions. A global variable has global scope which means it can be defined anywhere in the program. Example :
#include <stdio.h>

int x = 20;

void Myfunc() {
  x = 50;
  printf("Myfunc [x] : %d\n", x);

int main() {
  printf("Main [x] : %d\n", x);
  return 0;
Output :

Myfunc [x] : 50
Main [x] : 50

As we can see that the value of x is modified by the Myfunc() variable.

Next Topic :