TYPES OF VARIABLE IN C LANGUAGE | FULL INFORMATION

VARIABLE SCOPE IN C PROGRAMMING LANGUAGE

A variable can be used throughout the program. This is its scope. According to the scope, the variables are divided into two categories.


  • Local Variable
  • Global Variable


  • Local Variable - 


Local variables are variables that are defined in a small block of a program such as a block of functions or control statements, etc. The use of such variables remains limited to that block only. For example, if you have created a variable in a function, then you cannot use that variable outside that function.


Example :


#include<stdio.h>

void myFunction();

int main()

{

int num=6;

myFunction();

printf(“Num in main() : %d”, num);

return 0;

}


void myFunction()

{

int num= 5;

printf(“Num in myFunction : %d\n”,num);

}


This program generates output as follows.


Num in myFuntion() : 5


Num in main() : 6


  • Global Variable -  


Global variables are those variables that are scoped throughout the program. You can access these variables anywhere in the entire program. These variables are defined at the beginning of the program.


Example :


#include<stdio.h>

int num=5;   //global variable

void myFunction();

int main()

{

myFunction();

printf(“Num in main() : %d”,num);

return 0;

}


void myFunction()

{

printf(“Num in myFunction : %d\n”,num);

}


This program generates output as follows.


Num in myFunction() : 5


Num in main() : 5


AKASH TIMES

I AM AN ENGINEER BY PROFESSION BUT BLOGGER IS MY OLD DAYS DREAM TO CREATE MY OWN SITE FOR THOSE WHO ARE CURIOUS ABOUT MY BIRTHPLACE THEN I AM COMING FROM (INDIA). THE CURRENT CITY LIVES IN NAVI MUMBAI, INDIA

Post a Comment

IF YOU HAVE ANY PROBLEM PLEASE FELL FREE TO CONTACT US BY - AKASH TIMES

PREVIOUS POST NEXT POST