Local Variable And Global Variable
- The variable declared within the body of the function are called local variable,such variable can only be used within the body of function,within which they are created.
- They are created,when the function is called and destroy automatically when the function exit,We may also use the keyword "auto" to declare automatic variable explicitly.
- One important feature of automatic variable that,there value cannot be change in other functions of same program.This assure that we may declare and use the same variable name in different function in the same program without causing any confusing to the compiler.
- The variable declared outside of all the function usually before the main() function,are called global variable.Such variable can be used any where in the entire program.
- Local variable have higher precedence then global variable,If we have two variable of same name,one is local and another is global ,then local variable have higher precedence then the global variable.
img credit: Google images |
#include<stdio.h>
#include<conio.h>
main()
{ int x=5;
printf("x=%d",x);
fun1();
printf("\n Now x=%d",x);
getch();
}
fun1()
{int x=20;
x=x+10;
printf("\n x=%d",x);
}
Output-:
x=5
x=30
x=5
Program Of Local Variable-:
#include<stdio.h>
#include<conio.h>
int x=50;
main()
{ auto int x=5;
printf("x=%d",x);
fun1();
printf("\n Now x=%d",x);
getch();
}
fun1()
{
printf("\n x=%d",x);
}
Output-:
x=5
x=50
x=5
I hope My Blogs are helpful to you
ReplyDelete