What is scope of a variable ? Differentiate between Global and Local variables

By | August 2, 2016

What do you mean by scope of a variable ? Differentiate between Global and Local variables giving an example of each. 5m Dec 2009

 Scope of a Variable

Scope of a Variable means the bondaries of the variable in which it appears and can be accessible.

 

Global vs. Static variables:

Global variables are recognized through out the program whereas local valuables are recognized only within the function where they are defined.

 

External (Global) Variables

These are not confined to a single function. Their scope ranges from the point of declaration to the entire remaining program. Therefore, their scope may be the entire program or two or more functions depending upon where they are declared.

Points to remember:

• These are global and can be accessed by any function within its scope.

Therefore value may be assigned in one and can be written in another.

• There is difference in external variable definition and declaration.

• External Definition is the same as any variable declaration:

• Usually lies outside or before the function accessing it.

• It allocates storage space required.

• Initial values can be assigned.

• The external specifier is not required in external variable definition.

• A declaration is required if the external variable definition comes after the function definition.

• A declaration begins with an external specifier.

• Only when external variable is defined is the storage space allocated.

• External variables can be assigned initial values as a part of variable definitions, but the values must be constants rather than expressions.

• If initial value is not included then it is automatically assigned a value of zero.

 

Let us study these variables by a sample program given below:

Example /* Program to illustrate the use of global variables*/

 

# include <stdio.h>

int gv; /*global variable*/

main ( )

{

void function1(); /*function declaration*/

gv = 10;

printf (“%d is the value of gv before function call\n”, gv);

function1( );

printf (“%d is the value of gv after function call\n”, gv);

}

void function1 ( )

{

gv = 15: }

 

OUTPUT

 

10 is the value of gv before function call

15 is the value of gv after function call

 

Static (Local) Variables

In case of single file programs static variables are defined within functions and individually have the same scope as automatic variables. But static variables retain their values throughout the execution of program within their previous values.

Points to remember:

• The specifier precedes the declaration. Static and the value cannot be accessed outside of their defining function.

• The static variables may have same name as that of external variables but the local variables take precedence in the function. Therefore external variables maintain their independence with locally defined auto and static variables.

• Initial value is expressed as the constant and not expression.

• Zeros are assigned to all variables whose declarations do not include explicit initial values. Hence they always have assigned values.

• Initialization is done only is the first execution.

 

Let us study this sample program to print value of a static variable:

Example 8.6

 

/* Program to illustrate the use of static variable*/

#include <stdio.h>

main()

{

int call_static();

int i,j;

i=j=0;

j = call_static();

printf(“%d\n”,j);

j = call_static ();

printf(“%d\n”,j);

j = call_static();

printf(“%d\n”,j);

}

int call_static()

{

static int i=1;

int j;

j = i;

i++;

return(j);

}

 OUTPUT

1

2

3

This is because i is a static variable and retains its previous value in next execution of function call_static( ). To remind you j is having auto storage class. Both functions main and call_static have the same local variable i and j but their values never get mixed.

 

1. (a) Develop an algorithm, and write a program to print the Fibonacci series upto a given number using recursion. 10

Solved program can be found on this link http://cssimplified.com/c-programming/design-an-algorithm-draw-a-corresponding-flow-chart-and-write-a-program-in-c-to-print-the-fibonacci-series-10m-jun2006

 

(b) What do you understand by function prototype? Write a program in ‘C’ to calculate the GCD of three numbers using the function prototype. 10

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-gcd-of-given-numbers-using-recursion

 

 

(d) Differentiate between call by value and call by reference methods of parameters passing to a function giving an example of each. 5

Solved program can be found on this link http://cssimplified.com/c-programming/design-an-algorithm-draw-a-corresponding-flow-chart-and-write-a-program-in-c-to-swap-the-values-using-pass-by-value-and-pass-by-reference-methods-10m-jun2007

 

(e) What will be the output of following ‘C’ programme? 5

main()

{

int i = 1, j = 1;

for (;;)

{

if (i > 5)

break;

else

j + = 1;

printf (“In %d”, j)

i + = j;

}

}

 OUTPUT:

2

3

4

5

6