Difference between Static variable declared in different scopes

后端 未结 6 965
梦谈多话
梦谈多话 2020-12-19 12:27

What is the difference between declaring static variable inside a block and outside a block in a file? Eg, here, what is difference between static variables a,b,c,d? Can we

6条回答
  •  一生所求
    2020-12-19 12:48

    static int a;
    

    means the variable a is a file scope variable, i.e, it can't be seen from other files.

    void getVol(..)
     { 
       static int b;
     }
    

    means the local variable b has a life cycle that goes from the program starts to the program ends, i.e, you can assign it some value, while on the next call of the function, it remembers that value.

    c and d are similar to b.

提交回复
热议问题