Scope Rules

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −

  • Inside a function or a block which is called local variables.

  • Outside of all functions which is called global variables.

  • In the definition of function parameters which are called formalparameters.

  • Let us understand what are local and global variables, and formal parameters.

Local Variables

The variables declared inside the function are automatic or local variables.

The local variables exist only inside the function in which it is declared. When the function exits, the local variables are destroyed.

Program On Local Variables




Global Variables

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how global variables are used in a program.

Program On Global Variables




Storage Class Specifiers

Storage class specifiers in C language tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable.


storage_specifier data_type variable _name;

Types Of Storage Class Specifiers

  1. auto
  2. extern
  3. static
  4. register

Flowchart For Storage Classes



  • For faster access of a variable, it is better to go for register specifiers rather than auto specifiers.
  • Because, register variables are stored in register memory whereas auto variables are stored in main CPU memory.
  • Only few variables can be stored in register memory. So, we can use variables as register that are used very often in a C program.

auto Storage Classs

  1. This is default storage class
  2. All variables declared are of type Auto by default 
  3. In order to Explicit declaration of variable use ‘auto’ keyword
  4. auto int num1 ;   // Explicit Declaration 

Program On auto Storage Class




extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’, the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

Program On extern Storage Class




static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared.

In C programming, when static is used on a class data member, it causes only one copy of that member to be shared by all the objects of its class.

Program On static Storage Class


register Storage Class

  1. register keyword is used to define local variable.
  2. Local variable are stored in register instead of RAM.
  3. As variable is stored in register, the Maximum size of variable = Maximum Size of Register
  4. unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register.
  5. This is generally used for faster access.
  6. Common use is “Counter

Program On register Storage Class