Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 1 June 2019

Local variable vs global variable in C

Following are the differences between a local

 variable and global variable:

Local variable                                       
  • Declaration:
A variable which is declared inside the function
 or block is known as a local variable.


  • Scope:
The scope of a variable is available within a
 function in which they are declared.

  • Access:
Variables can be accessed only by those
 statements inside a function in which
 they are declared.

  • Life:
Life of a variable is  created  when the function
 the block is entered and destroyed on its exit.

  • Storage
Variables are stored in a stack unless
 specified.

  • Example:
void function1(){
int x=10;
}

    Global variable

  • Declaration:
 A variable which is declared outside function or block is known as a global variable.

  • Scope:
The scope of a  variable is available throughout the program.

  • Access:
Any statement in the entire the program 
can access variables.

  • Life:
Life of a variable exists until the program 
is executing.

  • Storage
The compiler decides the storage location
 of a variable.

  • Example:
int value=20;
void function1(){
int x=10;

0 comments:

Post a Comment