File Scope of Variable in C Programming Language

January 24, 2025 No Comments » Hits : 76






File Scope of Variable :


  1. File Scope is also called Global Scope
  2. It can be used outside the function or a block
  3. It is visible in entire program
  4. Variables defined within Global scope is called as Global variables

Definition : Variable is said to have global scope / file scope if it is defined outside the function and whose visibility is entire program
Live Example :

#include<stdio.h>
void message();
int num1 = 1; // Global 
void main()
{
int num1 = 6 ;
printf("%d",num1); // Local variable is accessed
message();
}
void message()
{
printf("%d",num1); // Global variable is accessed
}

Output :

  • 6 1

Explanation :

  1. Program have two functions main and message
  2. As num1 declared in main is local to main , so it can be accessed from main
  3. But num1 is not defined in the function message , then global variable is accessed