C语言 静态常量和常量有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13185751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between static const and const?
提问by Lior
What is the difference between static constand const?
For example:
static const和 和有const什么区别?例如:
static const int a=5;
const int i=5;
Is there any difference between them? When would you use one over the other?
它们之间有什么区别吗?你什么时候会使用一个?
采纳答案by ouah
The difference is the linkage.
区别在于联动。
// At file scope
static const int a=5; // internal linkage
const int i=5; // external linkage
If the iobject is not used outside the translation unit where it is defined, you should declare it with the staticspecifier.
如果i对象不在定义它的翻译单元之外使用,则应使用说明static符声明它。
This enables the compiler to (potentially) perform further optimizations and informs the reader that the object is not used outside its translation unit.
这使编译器能够(可能)执行进一步的优化,并通知读者该对象未在其翻译单元之外使用。
回答by Joe
staticdetermines visibility outside of a function or a variables lifespan inside. So it has nothing to do with constper se.
static确定函数外部的可见性或内部的变量寿命。所以这与它本身无关const。
constmeans that you're not changing the value after it has been initialised.
const意味着您在初始化后不会更改该值。
staticinside a function means the variable will exist before and after the function has ended.
static在函数内部意味着该变量将在函数结束之前和之后存在。
staticoutside of a function means that the scope of the symbol marked staticis limited to that .c file and cannot be seen outside of it.
static在函数之外意味着标记符号的范围static仅限于该 .c 文件,并且无法在该文件之外看到。
Technically (if you want to look this up), staticis a storage specifier and constis a type qualifier.
从技术上讲(如果你想查一下),static是一个存储说明符,const也是一个类型限定符。
回答by Tobin
const int i=5;
i value you can modify by using a pointer if i is defined and declared locally,
if it is static const int a=5; or const int i=5; globally , you can not modify since it is stored in RO memory in Data Segment.
const int i=5;
如果 i 是在本地定义和声明的,则可以使用指针修改 i 值,如果它是 static const int a=5; 或 const int i=5; 全局,您不能修改,因为它存储在数据段的 RO 内存中。
#include <stdio.h>
//const int a=10; /* can not modify */
int main(void) {
// your code goes here
//static const int const a=10; /* can not modify */
const int a=10;
int *const ptr=&a;
*ptr=18;
printf("The val a is %d",a);
return 0;
}
回答by nibot
It depends on whether these definitions are inside of a function or not. The answer for the case outsidea function is given by ouah, above. Insideof a function the effect is different, illustrated by the example below:
这取决于这些定义是否在函数内部。上面的 ouah 给出了函数外情况的答案。在函数内部,效果是不同的,如下例所示:
#include <stdlib.h>
void my_function() {
const int foo = rand(); // Perfectly OK!
static const int bar = rand(); // Compile time error.
}
If you want a local variable to be "really constant," you have to define it not just "const" but "static const".
如果您希望局部变量是“真正的常量”,那么您不仅要定义“const”,还要定义“static const”。

