top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How does program knows about static variable?

+2 votes

We all know that if we declare a static variable in a function then it will be created only once, in next call of the function static variable will not be created and uses its old value.

But my question is, how does our program knows that this variable is created or not?

posted Sep 24, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes
#include <stdio.h>

void foo()
    int a = 10;
    static int sa = 10;


     += 5;
        sa += 5;

        printf("a = %d, sa = %d\n", a, sa);

    int main()
        int i;

        for (i = 0; i < 10; ++i)

This prints:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60
answer Nov 4, 2015 by Shivaranjini
Similar Questions
+3 votes

As per my understanding uninitialized static variable is stored in BSS and initialized static variable is stored in DS, then what about the variable which is initialized with 0 ? where it will be stored BSS/DS?

+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.