Difference between Static variable declared in different scopes

后端 未结 6 964
梦谈多话
梦谈多话 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 13:01

    static int a;//file scope variable
    void getVol()
    {
        static int b;//fixed duration
    }
    

    File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files). A fixed duration variable is one that retains it’s value even after the scope in which it has been created has been exited! Fixed duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program. Check this link:http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/

提交回复
热议问题