Scope of variables

Difference between Local variable and Global variable

#In C language, a variable can be either of global or local scope.

Global variable

Global variables are defined outside of all the functions, generally on top of the program. The global variables will hold their value throughout the life-time of your program.

Local variable

A local variable is declared within the body of a function or a block. Local variable only use within the function or block where it is declare.

Example of Global and Local variable

Example

#include<stdio.h>
#include<conio.h>

int a;   // global variable
void main()
{
int b;    // local variable
a=10, b=20;
printf("Value of a : %d",a);
printf("Value of b : %d",b);
getch();
}

Output

Value of a: 10
Value of b: 20

0 comments: