Tutorial references

c
c++
c#

Tutorial references

chapters

Variable Scope in c

Variable Scope

In variable chapter you have read about variable

There are two type of variable according to scope:

  (1.) Local variable

  (2.) Global variable

Local variable

A variable which is declared inside of function or block that is called "Local variable".

Local variable can not be used outside of function or block.

In below program a and b variable declared inside of f1() therefore a and b is local variable.

Example

#include <stdio.h>
#include <conio.h>

void f1(){
 int a = 100;
 int b = 200;
 printf("\n value of a = %d",a);
 printf("\n value of b = %d",b);
}

void main(){
 clrscr();
 f1();
 getch();
}

Run program

Below program has an error and warning . In below program uses f1() and f2() function. Inside of f1() two variables is declared therefor a and b variable are local variable. But f2() wants to use b variable therefore program has an error because local variable can not be use outside of function or block.

Example

#include <stdio.h>
#include <conio.h>

void f1(){
 int a = 100;
 int b = 200;
 printf("\n value of a = %d",a);
}

void f2(){
 printf("\n value of b = %d",b);
}

void main(){
 clrscr();
 f1();
 f2();
 getch();
}

Run program

Below program uses m variable which is declared inside of if(){ } block therefore m is local variable.

Example

#include <stdio.h>
#include <conio.h>

void f1(){
 int a;
 printf("\n enter value for a: ");
 scanf("%d",&a);
if(a > 0){
 int m;
 printf("\n Enter value for m: ");
 scanf("%d",&m);
 printf("\n value of a = %d ",a);
 printf("\n value of m = %d ",m);
}
}

void main(){
 clrscr();
 f1();
 getch();
}

Run program

Below program has error. f1() uses an if(){ } block. m varible is decalred inside of if(){ } block therefore m is a local variable.But program uses m variable outside of if(){ } block therefore program has error because m variable can not be use outside of if(){ } block.

Example

#include <stdio.h>
#include <conio.h>

void f1(){
 int a;
 printf("\n enter value for a: ");
 scanf("%d",&a);
if(a > 0){
 int m;
 printf("\n Enter value for m: ");
 scanf("%d",&m);
}
 printf("\n value of a = %d ",a);
 printf("\n value of m = %d ",m);
}

void main(){
 clrscr();
 f1();
 getch();
}

Run program

parameter of a function is also called local variable. Therefore parameters can not be used outside of function.

Example

#include <stdio.h>
#include <conio.h>

void f1(int m, int n){
 printf("\n value of m = %d",m);
 printf("\n value of n = %d",n);
}

void main(){
 clrscr();
 f1(100,200);
 getch();
}

Run program

Global Variable

Global variable is always declared outside of function body.

Global variable can be use anywhere in program.

In below program n is declared outside of function therfore n is a global variable and global variable is used anywhere in program.

Example

#include <stdio.h>
#include <conio.h>

 int n = 100; /* global */
void f1(){
 printf("\n value of n = %d",n);
}

void f2(int a){
 printf("\n value of a = %d",a);
}

void main(){
 clrscr();
 f1();
 f2(n);
 getch();
}

Run program

Variable priority

Local variable and Global variable both are with same name then local variable priority is higher than global variable.

Example

#include <stdio.h>
#include <conio.h>

 int m = 100; /* global */

void f1(){
int m = 10; /* local */
 printf("\n value of m = %d",m);
}

void main(){
 clrscr();
 f1();
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018