Scope of Variable in C : Compile Error

Scope of Variable :


void message();

void main()
int num1 = 6 ;

void message()

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