Global Copy is Accessible in All the Functions : C Programming
Different Copies of Variables : Local Copy and Global Copy
#include<stdio.h> int num = 100 ; // Global void func(); // Prototype int main() { int num = 20; // Local to main { int num = 30 ; // Local to Inner Part printf("%d ",num); func(); } printf("%d ",num); return(0); } void func() { printf("%d ",num); }
Output :
30 100 20
Explanation : Scope of the Variable
We have 3 copies of the variable “num”. We have global copy,local copy of main function and local copy of inner block.
{ int num = 30 ; // Local to Inner Part printf("%d ",num); func(); }
in the above inner block, priority is given to the local variable i.e 30.
void func() { printf("%d ",num); }
after calling the above function from inner block we can have access to the global copy of variable since we don’t have local copy of variable.
printf("%d ",num); return(0);
after moving out from the inner block, we again have access to the local copy from main function.