我想在文件中声明常量,然后在特定源文件的函数调用中使用它们。这些常数必须对所有文件都不可见。如何在C语言中实现?
此问题的解决方案是否取决于编译器和IDE?
C中变量的限制范围(可见性和持续时间)很简单。关于该主题的教程很多,例如this one:(以下摘录列出了这些主题)
...了解C语言中数据的范围和存储类别。本课程涵盖的主题是
Block scope Function scope File scope Program scope The auto specifier The static specifier The register specifier The extern specifier The const modifier The volatile modifier
您所描述的内容暗示文件范围将对您有用。摘录自同一链接:
文件范围和范围层次结构>>
...
大多数大型程序由几个程序文件组成。
源代码的以下部分显示了具有文件范围的变量:
int x = 0; /* program scope */ static int y = 0; /* file scope */ static float z = 0.0; /* file scope */ int main() { int i; /* block scope */ . . . return 0; }