简体   繁体   English

在ANSI C中声明常量变量的标准是什么?

[英]What is the standard to declare constant variables in ANSI C?

I am teaching myself C by going over my C++ book and recoding the problems in C.我通过阅读我的 C++ 书并在 C 中记录问题来自学 C。 I wanted to know the correct industry standard way of declaring variables constant in C.我想知道在 C 中声明变量常量的正确行业标准方法。 Do you still use the #define directive outside of main, or can you use the C++ style const int inside of main?您仍然在 main 之外使用#define指令,还是可以在 main 内部使用 C++ 样式const int

const in C is very different to const in C++. C 中的const与 C++ 中的const非常不同。

In C it means that the object won't be modified through that identifier:在 C 中,这意味着不会通过该标识符修改 object:

int a = 42;
const int *b = &a;

*b = 12; /* invalid, the contents of `b` are const */
a = 12; /* ok, even though *b changed */

Also, unlike C++, const objects cannot be used, for instance, in switch labels:此外,与 C++ 不同,const 对象不能用于例如开关标签中:

const int k = 0;
switch (x) {
    case k: break; /* invalid use of const object */
}

So... it really depends on what you need.所以......这真的取决于你需要什么。

Your options are你的选择是

  • #define : really const but uses the preprocessor #define : 真正的 const 但使用预处理器
  • const : not really const const : 不是真的 const
  • enum : limited to int enum :仅限于int

larger example更大的例子

#define CONST 42
const int konst = 42;
enum /*unnamed*/ { fixed = 42 };

printf("%d %d %d\n", CONST, konst, fixed);

/* &CONST makes no sense */
&konst; /* can be used */
/* &fixed makes no sense */

Modern C supports both #define s and const globals.现代 C 支持#defineconst全局变量。 However, #define s are usually preferred for true constants;但是, #define通常更适合真正的常量; this is because #define s can be inlined into the place where they are used, while const globals generally require a memory read, particularly if they're defined in a different translation unit.这是因为#define可以内联到使用它们的位置,而const全局变量通常需要读取 memory ,特别是如果它们是在不同的翻译单元中定义的。

That said, complex constant structures are a good use for const globals - strings, struct s, arrays, etc.也就是说,复杂的常量结构非常适合const全局变量 - 字符串、 struct 、arrays 等。

In most modern implementations the compiler is trying to do more just to bare the access via the symbol by placing the global const variables in the read only sections.在大多数现代实现中,编译器试图通过将全局const变量放置在只读部分中来做更多的事情,以通过符号进行访问。 It actually protects them on many systems against change.它实际上在许多系统上保护它们免受更改。 Some examples: segfault on Linux systems and errors on Windows or placing the code in the FLASH when using the micro controllers.一些示例:Linux 系统上的段错误和 Windows 上的错误或在使用微控制器时将代码放在 FLASH 中。

It is of course 100% implementation, but it is good to know that modern machines with memory protection hardware and modern compilers do more than only follow the standard这当然是 100% 实现,但很高兴知道具有 memory 保护硬件和现代编译器的现代机器不仅仅遵循标准

Variables qualified by const are not considered compile-time constant , they have one significant limitation: const int cannot be used to define the size of an array.(historical reasons but not a limitation of the compiler, C++ corrects this oversight though)const限定的变量不被视为编译时常量,它们有一个重大限制: const int不能用于定义数组的大小。(历史原因,但不是编译器的限制,C++ 虽然纠正了这个疏忽)

You can choose:您可以选择:

#define SIZE 5   /* preprocessor */
enum { SIZE=5 }; /* compiler */

The standard that is followed in most C programs is to have all constants as macros (#define) in a separate header file.大多数 C 程序遵循的标准是将所有常量作为宏 (#define) 放在单独的 header 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM