Static variables in C and C++

前端 未结 5 1221
天涯浪人
天涯浪人 2021-02-07 08:33

Is there any difference between a variable declared as static outside any function between C and C++. I read that static means file scope and the varia

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 08:45

    No, there's no difference between C and C++ in this respect.

    Read this SO answer about what static means in a C program. In C++ there are a couple of other meanings related to the use of static for class variables (instead of instance variables).

    Regarding global vars being static - only from the point of view of memory allocation (they are allocated on the data segment, as all globals are). From the point of view of visibility:

    static int var;    // can't be seen from outside files
    int var;           // can be seen from outside files (no 'static')
    

提交回复
热议问题