Understand static keyword in best way

Static Keyword in C Programming

Let’s assume that there is a small family of 4 members(Mother, Father, Son and Daughter) in one house, and they are having a basket which contains 6 apples in it.

Now assume that basket is a variable and its value is 6 apples, and value of basket is common among all the family members.

Now… son throws one apple out of the window and value of the basket is reduced to 5 apple, and now for all the members is family the value of basket is 5 apples.

Now… Daughter throws one apple out of the window and value of the basket is reduced to 4 apple, and now for all the members is family the value of basket is 4 apples.

So we can see that even any member of the family is trying to change the value of basket then this change is static for all the members.

Because here basket is static variable for all the members in family.

In C program we can use static keyword to declare Variable and Functions in C.

ITVoyagers

Static Variable

Static variable maintain its value even when it is called out of its scope.

Syntax for declaring static variable.

   static data_type variable_name; // declaration

   static data_type variable_name = value; // initialization

   example:

   static int basket = 6;

Let's take an example

    #include<stdio.h>

    void fun1()

    {

        int i = 0;

        i++;

        printf(“Value of i is = %dn”,i);

    }

    void main()

    {

        fun1();

        fun1();

    }

Output:
 
Value of i is = 1                                                                                                          
Value of i is = 1 

In above example we have declare a function name fun1(). In that function we have initialized a variable i with value 0

On next line we are incrementing value of i by 1. By now value of i is 1.

On next line we are printing the value of i which is 1.

Now we call this function in main() function for two times, that means block of code written in fun1() function will get executed two times.

First when function is called compiler jumps to function fun1() and start executing it. First it will initialize i with value 0.

After that value of i will get incremented to 1, and after it will print value of which is 1.

And when execution of the function fun1() is complete i will get destroyed.

When second time the function is called then it will repeat all the above steps. This will be fixed even if you called fun1() for thousand times. Function fun1() will always print 1 as a value of i.

Now let’s make variable i static.

    #include<stdio.h>

    void fun1()

    {

        static int i = 0;

        i++;

        printf(“Value of i is = %dn”,i);

    }

    void main()

    {

        fun1();

        fun1();

    }

Output:
 
Value of i is = 1                                                                                                          
Value of i is = 2 

In above example we have declare a function name fun1(). In that function we have initialized a static variable i with value 0

On next line we are incrementing value of i by 1. By now value of i is 1.

On next line we are printing the value of i which is 1.

Now we call this function in main() function for two times, that means block of code written in fun1() function will get executed two times.

First when function is called compiler jumps to function fun1() and start executing it. First it will initialize static variable i with value 0.

After that value of i will get incremented to 1, and after it will print value of which is 1.

And when execution of the function fun1() is complete i will not get destroyed but it will remain in memory.

When second time the function is called then compiler will not initialized i with value 0 because i was not destroyed i was saved in memory because we declared i as a static variable. Static keyword allows us to save variable in memory while the program is running.

So when second when fun1() is called it will skip initialization part and increment the value of i by after increment the value of i becomes 2 and that is why it will print value of i is 2 in second call.

Let's take another example

    #include<stdio.h>

    void fun1()

    {

        static int i = 0;

        i++;

        printf(“Value of i is = %dn”,i);

    }

    void fun2()
    {
        fun1();

    }

    void main()

    {

        fun1();

        fun2();

        fun2();

    }

Output:
 
Value of i is = 1                                                                                                          
Value of i is = 2 
Value of i is = 3

In above example we have added one more function in example i.e. fun2(). Function fun2() calls fun1()

In main function we are calling fun1() first and fun2() two times after that. In result we can see that even if we are printing value of i using function fun2() still the value of i is increasing.

This shows that value of is common for all the functions in program.

Let's take one more example

    #include<stdio.h>

    static int i = 0;

    void fun1()

    {

        i++;

    }

    void fun2()
    {
        i++;

    }

    void main()

    {

        fun1();

        printf(“Value of i is = %dn”,i);

        fun2();

        printf(“Value of i is = %dn”,i);

        fun2();

        printf(“Value of i is = %d”,i);

    }

Output:
 
Value of i is = 1                                                                                                          
Value of i is = 2 
Value of i is = 3

In above example we have two functions fun1() and fun2(), both the functions increments the value of i.

We are calling both the functions in main() function and we can see when fun1() increments the value of static variable i to 1, and when fun2() increments the value of i then value of get increments to 2.

This means that static variable is common for all.

Default value of static variable is zero

In C programming default value of declared variable is garbage value, but if we declare the variable with static keyword then the default value of the variable is 0.

Let's take an example

    #include<stdio.h>

    void main()

    {

        static int x;
        int y;
        printf(“Value of x is = %dn”,x);

        printf(“Value of y is = %d”,y);

    }

Output:
 
Value of x is = 0 
Value of y is = 13

Static variables can only be initialized by constant literals.

    #include<stdio.h>

    int fun1()

    {

        return 10;

    }

    void main()

    {

        static int x = fun1();

    }

Above example will generate error because static variable can only be initialized with constant literals.

Static Function

We can also declare function as static.

To declare function as static we have to write static before return type.

    Syntax:

    static return_type function_name()

    {

         statement;

     }

    Example:

    #include<stdio.h>

    static int fun1()

    {

        return 10;

    }

When we declare function as a static then it is restricted to that particular file.

In simple terms if we create .c(example a.c) file and declare a static function in it, then it not possible to call this function in another .c(example b.c) file.

Let's take an example

    // This function is declare in a.c file

    #include<stdio.h>

    static int fun1()

    {

        return 10;

    }

    // This above function is called in b.c file

    #include<stdio.h>

    void main()

    {

         fun1(); // This is not allowed when if called function is in another file and it is declared using static keyword

    }

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

PRACTICALS IN C PROGRAM

Leave a Comment