C Tutorial C References

C - Scope of Variables



A variable can only be accessed in the area in which it is defined, which is called scope of the variable. In C, there are mainly three types of variable scopes:

  • Local scope
  • Global scope
  • Function Prototype Scope

Local Scope

If a variable is created inside a function or a block { .. }, it is called local variable and has local scope. A local variable can only be used inside that function or block.

Example

In the example below, a local variable called MyInt is created and used inside the main function.

#include <stdio.h>

int main () {
  //local variable created 
  //inside main function
  int MyInt = 100;

  printf("%i\n", MyInt); 
  return 0;
}

The output of the above code will be:

100

Local variable outside function

A local variable can not be used outside the block/function in which it is defined and can be used only inside that block/function.

Example

In the example below, a local variable called MyInt is created inside the main function and user-defined function called MyFunction. When MyInt is printed, it gives different results.

#include <stdio.h>

void MyFunction() {
  //local variable created inside
  //MyFunction function. It is only
  //accessible inside this function
  int MyInt = 500;
  printf("%i\n", MyInt);
}

int main () {
  //local variable created 
  //inside main function
  int MyInt = 100;

  printf("%i\n", MyInt);
  MyFunction(); 
  return 0;
}

The output of the above code will be:

100
500

Global Scope

If a variable is created outside all functions, usually on top of the program, it is called global variable and has global scope. A global variable can be used anywhere, inside the function and outside the function.

Example

In the example below, a global variable called MyInt is created and used inside the main and MyFunction functions.

#include <stdio.h>

//global variable created outside
//all functions
int MyInt = 100;

void MyFunction() {
  //accessing global variable
  printf("%i\n", MyInt);
}

int main () {
  MyFunction(); 

  //accessing global variable
  //and modifying it
  MyInt = 1000;
  MyFunction(); 

  return 0;
}

The output of the above code will be:

100
1000

Local Variable with same name as Global Variable

If a variable with same name as global variable is created inside the function, it will be a local variable for that function. Any operation performed on this local variable will not change the global variable. Please note that when a function contains local and global variable with same name, the local variable will take precedence over global variable.

Example

In the example below, a global variable called MyInt is created. A local variable with the same name is also created inside the main function. Any operation performed on this local variable do not change the global variable.

#include <stdio.h>

//global variable created outside
//all functions
int MyInt = 100;

void MyFunction() {
  //accessing global variable
  printf("%i\n", MyInt);
}

int main () {
  MyFunction(); 

  //creating local variable
  //with same name
  int MyInt = 500;
  MyFunction(); 

  printf("%i\n", MyInt);
  return 0;
}

The output of the above code will be:

100
100
500

Function Prototype Scope

The scope of the these variables begins right after the declaration in the function prototype and runs to the end of the declarations list. These scopes don’t include the function definition, but just the function prototype.

Example

The example below shows all types of scope of variables.

#include <stdio.h>

//global scope
int MyInt = 100;

//function prototype scope
void MyFunction(int MyInt) {
  MyInt = MyInt + 10;
  printf("%i\n", MyInt);
}

int main () {
  //local scope
  int x = 500;

  printf("%i\n", MyInt);
  MyFunction(x); 
  printf("%i\n", x);
  return 0;
}

The output of the above code will be:

100
510
500