Function: Global and Local Variables, Variables and Storage Class

Get unlimited access to the best preparation resource for UGC Electronic-Science: fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 183K)

Image shows types ofl variables

Image Shows Types Ofl Variables

Image shows types ofl variables

Global and Local Variables

The variable declared in a program may broadly be classified into following two types.

Image of Global and Local variables

Image of Global and Local Variables

Image of Global and Local variables

Local Variable

  • A variable declared within the body of a function will be evaluated only within the function.

  • The portion of the program in which a variable is retained in memory is known as the scope of the variable.

  • The scope of the local variable is a function where it is defined. A variable may be local to function or compound statement.

Global Variable

  • A variable that is declared outside any function is known as a global variable.

  • The scope of such a variable extends till the end of the program.

  • These variables are available to all functions which follow their declaration.

  • So, it should be defined at the beginning, before any function is defined. If in a program, variable a is declared as local as well as global.

Example 4

# include <iostream.h>

int m = 2.

void main ()

{

int m = 15.

{

int

cout < < “m =” < < m < < “\n”.

cout < < “: m =” < <: m < < “\n”

}

cout < < “m =” < < m < < “\n”.

cout < < “: m =” < <: m < < “\n”.

}The output of the above program is

m= 20

: m = 2

m = 15

: m = 2

Variables and Storage Class

  • The storage class of a variable determines which parts of a program can access it and how long it stays in existence.

  • The storage class can be classified as

Image of Variables and Storage class

Image of Variables and Storage Class

Image of Variables and Storage class

Developed by: