fuddlepixelFuddlepixel coz tech is fun();

Static Variables

Let us discuss in detail about the Static Variables. How variables can be made undestructable, what does it actually mean by undestructable variables ?

By Shubham Ramdeo
01 November 2015 · 5 mins read
# c  newbie 

Static Variables

That’s how we can make destructive variables, using Static variables !

Welcome to the Newbie Programmer Series. In the last post (click here) we have learned how can we use variables across files. we have learned using the ‘extern’ to make that possible. Lets discuss some more interesting things. So if you are new to this series, please go to index (click here), and read out all the previous parts so that you can easily understand what I am writing about.

So, we are discussing Static Variables. The word “Static” Means “fixed”. For an example, content printed on a book is static.

And as the name suggest, Static Variables are those variables which remains “alive” until the program ends.

This is gonna be a long post I think, please continue reading below

The thing is like when we initialize a variable, like int x; Then whenever this variable needs to be used, in the computer’s memory, a space is made for a variable x. And when its usage ends, its removed.

Hence each time, variable is created again and then removed.

For example see this piece of code below,

#include <stdio.h>
main()
{
    int i;
    for ( i = 0; i < 3; i++)
    {
        int x = 0;
        x++;
        printf("x = %d \n", x);
    }
}

OUTPUT :

x = 1
x = 1
x = 1

A loop is running three times.

Each time, a variable x is created and assigned a value 0. Then its incremented. i.e added 1 into x.

And then the value is printed. So x was 0, and incremented by 1. So new value of x is 1. So x = 1 is printed.

This loop runs three times so the message is printed three times.

As you can see, in the case like above, when a loop end, the value of x becomes 1. But when the next loop comes up, it’s again setup to x = 0. Means every time, variable is created and destroyed and have nothing to do with the previous values.

What we want is to let the computer “remember” the last value. So instead of creating new, we want that computer must check if there is any previous value and if there is, then use the old one.

So in that case, we use static. Which are those variable who do not die !

Its Usage is simple. Whatever you want to make a static variable, just write :

static name ;

So lets change the above program, See this below :

#include <stdio.h>
main()
{
    int i;
    for ( i = 0; i < 3; i++)
    {
        static int x = 0;
        x++;
        printf("x = %d \n", x);
    }
}

OUTPUT :

x = 1
x = 2
x = 3

This time, I have made the “x” variable, a static variable. So now, first loop starts. There was no “x” so a variable “x” is created and assigned a value “0”. Then its incremented. Now x = 1. Then the message is printed. Then second loop starts. As it’s a static variable, so computer search for any previous value. And so x got its last used value, x = 1. 

Then x is incremented, so now x = 2. Then the message is printed.

Third loop starts. This time, last value was x = 2. So on increment, x becomes 3. And the message x = 3 is printed.

You yourself compare both the above codes. They are exactly same, but one got “int x” and other got “static int x” And you can see it made a huge difference in the outputs !

Some notes regarding Static Variables

Lets discuss some interesting facts regarding static variables.

FUNDAMENTAL RULE : A space of a static variable is created only a once in the computer memory and remain there until the program ends.

You cannot use normal and static variables together. It will cause errors.

So if you are doing things like int x and static int x together, it may create troubles.

Value of a Static Variable is zero or null by default.

We may get some random values on simply declaring like “int x”, “float y”, “char name” All these might have some random value. But if the variable is declared using static, it will always zero or null by default.

For example :

#include <stdio.h>
main()
{
static int x;
static float y;
static char name;
printf("%d, %f, %c" , x, y, name);
}

OUTPUT :

0 0.000 0

But it does not go back to zero if already has a value.

So for example :

#include <stdio.h>
main()
{
static int x = 5;
static int x;
printf("%d" , x);
}

OUTPUT :

5

At first we declared x = 5 as static. In the next line, we again made a variable. But then according to the previous point, x should become zero but this does not happens as we are using static variable, the last value is saved.

Static Variables follow the scope rules.

So If a static variable is created in a function, its scopes remains in that function. i.e, it cannot be used by any other function. (called as local Variables.)

And if a static variable is created outside any function, then it can be used by any function. (called as global variables)

==

Pretty long post !

So we have discussed a good amount of things. I think that’s enough for today. We will discuss the real world applications later, until then, keep reading and stay connected.


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top