Block Scope of Variable in C Programming

January 24, 2010 No Comments » Hits : 166





Block Scope of Variable :


  1. Block Scope is also called Local Scope
  2. It can be used only within a function or a block
  3. It is not visible outside the block
  4. Variables defined within local scope is called as Local variables

Definition : Variable is said to have local scope / block scope if it is defined within function or local block
 Live Example :

#include<stdio.h>

void message();

void main()
{
 int num1 = 0 ;     // Local to main
 printf("%d",num1);
 message();
}

void message()
{
 int num1 = 1 ;         // Local to Function message 
 printf("%d",num1);
}

Output:

0 1

Explanation :

  1. Program have two functions main and message
  2. In both functions we have declared variable ‘num1′
  3. Both the variables have scope limited within the function body
  4. Local variables are printed as output.
  5. In main value 0 is assigned to num1
  6. In function message value 1 is assigned to num1

Incoming search terms: