Scope of Variable in C : Compile Error

Scope of Variable :

#include"stdio.h"

void message();

void main()
{
int num1 = 6 ;
printf("%d",num1);
message();
}

void message()
{
printf("%d",num1);
}


Output of Above Program :
  • Compile Error : Variable num1 is visible only within main function , It cannot be accessed by other function

Definition of Scope of Variable  : 
Scope of variable is defined as Region or Part of Program in which the variable is visible / accessed / valid .


Rules of Scope of Variable : 
  1. Local Variables are accessed / Visible only in Local Block
  2. Global variables can be explained from anywhere in program
  3. Priority is Given to Local variable if the two variables are declared with same name,type but in different blocks/functions

    Bookmark & Share