Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

What is scope and lifetime of a variable in C language

Welcome to your next tutorial, here we would be discussing about the scope and lifetime of a variable, and how exactly they are similar yet different from each other. We'll try to understand all this by using an example to understand the concepts in a better way.

Scope and life time of a variable in C programming language

So, let's understand each of these :

Life Time - Life time of any variable is the time for which the particular variable outlives in memory during running of the program.

Scope - The scope of any variable is actually a subset of life time. A variable may be in the memory but may not be accessible though. So, the area of our program where we can actually access our entity (variable in this case) is the scope of that variable.

The scope of any variable can be broadly categorized into three categories :

  • Global scope : When variable is defined outside all functions. It is then available to all the functions of the program and all the blocks program contains.
  • Local scope : When variable is defined inside a function or a block, then it is locally accessible within the block and hence it is a local variable.
  • Function scope : When variable is passed as formal arguments, it is said to have function scope.

Let's understand this with help of an example :

Consider this snippet of program :

#include<stdio.h>
int global = 100;          // global variable declared
void main()
{
   int local = 10;         // local variable declared
   printf("Global variable is %d",global);
   printf("Local variable is %d",local);
   func1();

}
void func1()
{
   printf("Global inside func1 is %d",global);  // Would print the global variable successfully.
}

Here we have a variable named global which is defined outside all the functions (generally at the top of the program) , it is defined even outside main, and hence would be available to every function defined in the program , here we consider only one function func1() .

The output of the above program would be :

Global variable is 100

Local variable is 10

Global inside func1 is 100

Let us consider the same program again but this time, let us change the definition of our function func1() as:

void func1() 
{ 
   printf("Global inside func1 is %d",global); // Would print the global variable successfully. 
   printf("Local inside func1 is %d", local); /* It would produce an error, because local
                                                  is a local variable and can be accessed 
                                                  only inside main function. */
}

Clearly, as mentioned in the comment, the printing of global variable would be done, but that of local variable would produce an error, as the variable named local is actually local to the function main() and hence can be accessed only through that function not from any other function, but for global variable, it can be accessed by all functions/ blocks a program is using.

Here our variable named global has global scope and lifetime, hence it outlives in memory till the program execution is completed and can be accessed by any function / block locally too. But in case of our local variable, it may outlives in memory till program execution is completed but it can be accessed from only within  the function or block it is defined in.

The basic problem here can be, we can have variable with same name defined as a global as well as local variable, and hence, it would be ambiguos as to which variable we want to actually refer to. We can resolve this issue in C++ (Advancement of C language) by the use of a special operator known as scope resolution operator. We'll look into this, when we'll cover tutorials for C++.

I hope all the concepts of scope and lifetime are now clear, but still if you have any doubt/ suggestion, please let us know through the comments section below.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy