Scope of variable in C | Local variable in C | Global variable in c

Preview
You must first complete What is a variable in C Language before viewing this Lesson

Scope of variable in C

Scope is defined as the lifetime of a variable. It specifies how long a variable will exist in the program and in what parts of a program, it will be available. Variables are categorized into two categories depending upon their scope.

  • Local variables
  • Global variables



 a.  Local variable  in C

A variable declared within a function is known as local variable. The local variable exists only within the function in which it is declared. They are unknown to other functions.

They are implemented using a stack. When the program control leaves the function, the local variables of the function are automatically destroyed.

They are recreated each time a function is executed or called. In c language local variable declaration must be first statement inside the function body.

 Program to demonstrate local variables.
#include<stdio.h>
void show()
{
int x=10; /*x is a local variable of function show()*/
printf(“x=%d”,x);
}
int main()
{
int a=4; /*a is a local variable of function main()*/
printf(“a=%d”,a);
show();
return(0);
}
Output
Welcome
a=4
x=10



 b. Global variable

A variable declared before function definitions is known as global variable.

Global variables are mostly declared above the main() function.

They retains their value throughout the program and any changes made to them will affect the whole program. They are allocated memory from data section of the memory.

We can access global variables in any function of a program and they even retain their values throughout the program.

In case a global variable and local variables have same name, preference will be given to the local variable.

Once a variable as been declared as global, any function can use it and change its value and subsequent functions can use the new value.

The very main feature of global variable is that it is visible from the point of declaration to the end of the program. Global variables are automatically initialized to zero.

The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, it may become difficult to understand the use of the variable in a large program.

They can lead to problems of naming because a global variable makes a name dangerous to use for any other local variable.

A local variable of the same name can prevent the use of global variable from access, again making the program code hard to understand.

The use of global variable makes it more difficult to isolate units of code for purposes of unit testing, so they can make the quality of the code low.

Program to demonstrate global variables.
#include<stdio.h>
int a=10; /*a is a global variable having value 10*/
void show()
{
printf(“\nWithin function a=%d”,a);
a++;
}
int main()
{
printf(“Within main() a=%d”,a);
++a;
show();
printf(“Within main() a=%d”,a);
return(0);
}
Output
Within main() a=10
Within function a=11
Within main() a=12
Description

In the above program, global variable a is assigned the value 10.

Within the main() function, value of a is incremented by 1.

Then function show() is called in which value of a is shown which is 11 after displaying the value of a, value of a is again incremented by 1.

Then program control is transferred again to main() function where value of a is shown which is shown as 12.

Best Books of C





Lesson tags: global variables in c, local variables in c, program of global variable, program of local variable, variable scope in c
Back to: C Programming Language