在C和C静态变量++ [英] Static variables in C and C++
问题描述
是否有声明为变静态
C和C ++之间的任何函数外任何区别。我读静态
表示文件的范围和变量将不会是文件外部访问。我也看到,在C,全局变量静态
。这是否意味着,在C全局变量不能在另一个文件被访问?
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 variables will not be accessible outside the file. I also read that in C, global variables are static
. So does that mean that global variables in C can not be accessed in another file?
推荐答案
没有,但在这方面的C和C ++之间没有差异。
No, there's no difference between C and C++ in this respect.
读<一个href=\"http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program/572550#572550\">this SO回答什么静态
在C程序的手段。在C ++中有与使用静态
的类变量(而不是实例变量)。
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')
这篇关于在C和C静态变量++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!