Right Click to Search

Sunday, July 11, 2024

What is Local Variable and Scope in C Programming?




Local Variable : 
  • Local Variable is Variable having Local Scope.
  • Local Variable is accessible only from function or block in which it is declared .
  • Local variable is given Higher Priority than the Global Variable.
Above Fig.  Tells us that - Above Program has 2 blocks
  1. Inner Block
  2. Outer Block.
Facts :
  1. Variables declared inside outer block are visible or meaningful only inside outer block.
    • Example : var1 is Local to Outer Block.
    • var1 cannot be accessed from its outer block.
    • var1 cannot be accessed from Other Function or other block.
    • var1 can be accessed from inner block
  2. Similarly Variables declared inside inner block are visible or meaningful only inside Inner block.
  3. Variables declared inside inner block are not accessed by outer block . i.e
#include<stdio.h>
void main()
{
int var1=10;
  {
  int var2 = 20;
  printf("%d %d",var1,var2); // Legal : var1 can be accessed
   }
printf("%d %d",var1,var2);   // Error : var2 is not declared
}

In Short -
  1. Inner Block can access variables declared inside Outer Block
  2. Outer block can't access variables declared inside Inner Block

Tags / Keywords : |

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates