Skip to main content
To Get All Information of Sarkari Jobs | Results | Admit Card | And courses ,Internships then CLICK HERE

Local Variable And Global Variable

 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.
techprogramiz.blogspot.com
img credit: Google images


Program Of Local Variable-:

#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



Comments

Post a Comment

Please do not enter any spam link in the comment box.


Download The Book "Let Us C" By Yashavant P.Kanetkar,Click The Below Button

-->Email Subscription

Enter Your Email Address:-

Delivered by FeedBurner

Copyright © 2020 TechProgramiz|All Right Reserved|Made By-: Sudarshan Pandey