Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope vs life of variable in C

Tags:

c++

c

Could someone exactly explain the concept of scope and life of variable in C. Is it different in C++? I'm confused between scope and life.

like image 934
Jitendra Avatar asked Oct 03 '11 07:10

Jitendra


People also ask

What is lifetime of a variable in C?

The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between allocation and deallocation of memory). In C language, a variable can have automatic, static or dynamic lifetime. Automatic − A variable with automatic lifetime are created.

What is scope visibility and lifetime of variables in C?

The scope of a declaration is the part of the program for which the declaration is in effect. C/C++ use lexical scoping. The lifetime of a variable or object is the time period in which the variable/object has valid memory. Lifetime is also called "allocation method" or "storage duration."

What do you understand by the scope and lifetime of a variable?

The scope of a variable refers to the areas or the sections of the program in which the variable can be accessed, and the lifetime of a variable indicates how long the variable stays alive in the memory.

What is the scope of a variable in C?

In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.


2 Answers

"Scope" of a variable is a region of source code, where you can refer to that variable.

"Lifetime" is how long it exists during the program execution.

By default the lifetime of a local variable is the same as its scope:

void foo()
{
    int x = 123;
    cout << x << endl;
    x += 1;
}

int main(){ foo(); foo(); foo(); }

Here, each time foo is called a new x is created (space is reserved for it on the stack), and when the execution leaves the block where x was declared, x is destroyed (which for int just means that the space that was reserved, now is freed for reuse).

In contrast:

void foo()
{
    static int x = 123;
    cout << x << endl;
    x += 1;
}

int main(){ foo(); foo(); foo(); }

Here, since x is declared static, space is reserved for x before the program execution even begins. x has a fixed location in memory, it's a static variable. And C++ has special rules about the initialization of such a variable: it happens the first time the execution passes through the declaration.

Thus, in the first call of foo this x is initialized, the output statement displays 123, and the increment increases the value by 1. In the next call the initialization is skipped (it has already been performed), the value 124 is output, and the value is incremented again. So on.

The lifetime of this x is from start of execution to end of execution.

like image 140
Cheers and hth. - Alf Avatar answered Oct 23 '22 07:10

Cheers and hth. - Alf


Scope is the region where the variable is accessible.
Life time is the time span during which an object remains valid.

An simple example:

#include <iostream.h>

void doSomething()
{
    x = 5; //Error! Not Accessible
}

int main() 
{

      int x = 4;
      std::cout<< x << endl;
      {
            int x = 2;
            cout << x << endl;
      }
      doSomething(); 
      std::cout<< x << endl;
      return 0;
}

The above gives the output:

4
2
4

In above program,
lifetime of variable x = 4 is throughout the main, i.e: It remains alive throughout the execution of the main, Also it is accessible within the main, that is its scope. Note that it is not accessible in the function because it is beyond the scope of the variable x.

while scope and lifetime of variable x = 2 is within the enclsing braces{ } inside the main.

like image 37
Alok Save Avatar answered Oct 23 '22 08:10

Alok Save