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

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 :

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:

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.

Bot who codes! A FOSS Contributor. www.github.com/CuriousLearner

3 Comments

  1. Well Structured Blog and super cool content, made my day.Thanks for this amazing article and keep doing the good job

    • Thanks for the appreciation Krishna. Please feel free to explore more content on TheGeekyWay and keep learning!

  2. So, life of any variable is till the completion of the program execution?

Leave a Reply

Your email address will not be published. Required fields are marked *