Auto, static and global variable in C/C++ programming

 

Auto variable

An auto variable is a variable which is declared within the scope of a function. We call it local variable as well. Auto variables are automatically created on the stack as soon as the program control enters the function and are destroys as soon as the program control returns from the function.

For example, in the below program variable var is declared within the function foo, and hence an auto variable.

#include
#include 

int foo()
{
    int var = 10;
    printf(" var has been created and is %d\n", var);

    return 0;
}

int main()
{
    printf("var doesn't exists in memory here\n");
    foo();
    printf("var has been destroyed and doesn't exists anymore.\n");
    return 0;
}

In the above code the variable var doesn’t exist in memory before the program control enters the function foo in line number 6. Once the function foo is entered var is created on the stack of the program, it remains allocated in the stack as long as the control remains inside foo. Once the control goes out of foo i.e. foo returns, var is destroyed and the memory which was allocated for foo in the stack is deallocated.

Some facts about auto variable:

  1. Auto variables are created on the stack. Memory is automatically created/allocated by the program, so no explicit allocation call need to be made to allocate space for the auto variable. Similarly, the auto variables are deallocated automatically when the function exits. So no explicit deallocation call is required.
  2. The life of an auto variable is not throughout the program. It is restricted to the execution of the function where it is created. Auto variable comes into life when the function is called and dies when the function exits.
  3. Scope of an auto variable is restricted to the function. Once the function exits the variable goes out of scope.

Static Variable

Static variables are somewhat special variable in C/C++. Static variables can be declared within a function or outside any function. When declared within a function body, static variables retain their values across function calls. Lets take an example to elaborate that point.

#include
#include 

int foo()
{
    static int var = 10;
    var++;

    return var;
}

int main()
{
    int temp;

    temp = foo();
    printf(" var = %d\n", temp);

    temp = foo();
    printf(" var = %d\n", temp);

    temp = foo();
    printf(" var = %d\n", temp);

    return 0;
}

If you run this program you will see the below output:

prog_output1.png

Now if you notice function foo has been called twice. In each call foo increases the value of var by 1. The initial value set to var is 10. So in the output we see the values 11, 12 and 13.  Notice the function foo is retaining the previous value of var each time the program control enters function foo and increases it by 1. This wouldn’t have been possible if var was a auto variable. Lets take a look at the below program:

#include
#include 

int foo()
{
    int var = 10;
    var++;

    return var;
}

int main()
{
    int temp;

    temp = foo();
    printf(" var = %d\n", temp);

    temp = foo();
    printf(" var = %d\n", temp);

    temp = foo();
    printf(" var = %d\n", temp);

    return 0;
}

In this case the variable var has been declared auto. The main function calls the function foo 3 times. But as the variable is now auto, it doesn’t retain its previous value anymore.  So the variable var is recreated every time function foo is entered and initialized to 10. So every time foo returns 11 and main prints 11 in each of these 3 calls to foo.

As you can see in case of auto, the variable is created every time  the function is entered, and destroyed when the function returns. As a result auto variable cannot retain it previous value. Whereas the static variable is created on the data segment of the program as opposed to the stack. If the static variable is not initialized then it goes to a section called BSS (Block Stated by Symbol).  When placed in this section the uninitialized static variable automatically initialized to 0. Hence, you do not need to initialize a static variable by 0 explicitly.

Some facts about static variable:

  1. Static variable can be declared within a function or outside of any function.
  2. If declared within a function body the scope of the static variable is the function body. Any reference to the same static variable outside the function where is has been declared will generate a compiler error.
  3. If declared outside all the functions, i.e. globally then the scope of the static variable is within the file where it is declared. The variable cannot be referenced outside the file. However, the variable can be referenced from anywhere in the same file.
  4. Life of a static variable is throughout the program, i.e. the variable doesn’t get destroyed ever once created.

Global Variable

Global variables are declared outside all the functions in the file. Global variables can referenced from anywhere in the file or outside the file. While accessing a gloabl variable from outside the file we use declare it as extern. Global variables are created in the data section of the program. Lets take a look at the below program:

#include
#include 

int var = 10;

void foo()
{
    printf(" var = %d\n", var);
    var++;
}
int main()
{
    int temp;

    var++;

    foo();

    printf(" var = %d\n", var);

    return 0;
}

prog_output3.png

In the above example as you can see the variable var has been declared as global (outside the scope of any function in the program). As a result it is accessible by the function main (line number 15). In line number 15 main() function increases the value of var by 1. The increment is visible by the function foo(). When foo is entered in the program sequence, foo prints the value of var as 11, which mean the change main() made to var is visible to foo as well. Now, foo increments the value further by 1. Changes made to var by foo is visible to main(). That’s why when main accesses var again, it finds the value to have been modified to 12 and prints it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s