简体   繁体   English

C常数

[英]C Constants

If I declare a constant say 如果我宣布一个不变的说法

"#define MONTHS =12" in CI am aware that pre-processor directive will replace whereever MONTHS is used, but want to know if a memory is allocated to store 12??, if yes, what would be the label and what would be the datatype. CI中的“#define MONTHS = 12”意识到预处理器指令将替换使用MONTHS,但是想知道是否将内存分配给存储12 ??,如果是,那将是什么标签以及将是什么数据类型。

You most likely want your define as such: 您最有可能希望您的定义如下:

#define MONTHS 12

/* some code here... */

int payAnnual = payMonthly * MONTHS;

To answer your question, no memory will be used. 要回答您的问题,将不会使用任何内存。 The pre-processor is unaware of such concepts as variables and memory. 预处理器不知道诸如变量和存储器之类的概念。 It is essentially an automated text editor. 它本质上是一个自动文本编辑器。 It will replace any occurrence of the symbol MONTHS with 12. 它将用符号MONTHS替换任何出现的符号。

Since the pre-processor is so dumb, it is generally preferable to use a const variable. 由于预处理器是如此愚蠢,通常最好使用const变量。 This gives you the benefit of type-checking, and can make compiler errors easier to read. 这为您提供了类型检查的好处,并且可以使编译器错误更容易阅读。 And so long as you declare it static, the variable will be optimized away. 只要你声明它是静态的,变量就会被优化掉。 (If you don't declare a global variable static in C, by default, it will be exported, so the compiler can't optimize it away entirely.) (如果你没有在C中声明一个静态的全局变量,默认情况下,它将被导出,因此编译器无法完全优化它。)

static const int MONTHS = 12;

The syntax is actually: 语法实际上是:

#define MONTHS 12

No = sign allowed. 否=允许签名。 And no memory is allocated in the compiled code for MONTHS, and it has no data type. MONTHS的编译代码中没有分配内存,也没有数据类型。

The preprocessor simply makes symbolic changes to the source code. 预处理器只是对源代码进行符号更改。 Hence the code would behave the same as if you had simply replaced all occurrences of MONTHS with 12 yourself. 因此,代码的行为与您自己用12替换所有MONTHS的行为相同。 And consequently there is no memory impact in using it. 因此,使用它不会对内存产生影响。

The use of pre compiler definitions is a great way of raising the level of abstraction of your code without memory of performance impact. 使用预编译器定义是提高代码抽象级别的一种很好的方法,而不会影响性能。

Regards 问候

No memory is allocated for preprocessor symbols on their own. 没有为预处理器符号分配内存。 If you ever use it as a value somewhere it can cause relevant allocation. 如果您曾在某处使用它作为值,则可能导致相关分配。

For example: 例如:

char something[1024*MONTHS];// 1024*12 bytes

otherwise the constant never outlives the compilation time. 否则常量永远不会超过编译时间。 And it has no datatype. 它没有数据类型。

Isn't #define - simply find n replace by the preprocessor before it hits the compilation stage.. It replaces all instances of the symbol with the value throughout the source. 不是#define - 只需在预编译器到达编译阶段之前找到n替换它。它用整个源中的值替换符号的所有实例。 Then the compiler takes over. 然后编译器接管。
So don't think memory is allocated. 所以不要以为内存是分配的。

The whole point about the C pre-processor is that it makes it's replacements before any code is compiled. 关于C预处理器的全部意义在于它在编译任何代码之前使其成为替代品。 so #-defined constants aren't stored at the point they are defined, only when they are used. 所以#defined常量不会存储在它们定义的位置,只有在它们被使用时才存储。

They are used just like an actual integer, and the normal literal type for an integer is used in calculations where the macro name appears in the program. 它们像实际整数一样使用,并且整数的普通文字类型用于计算中宏名称出现在程序中的计算中。

Just as if you were to declare 就像你要申报一样

#define FOO "bar"

Then occurences of FOO would be replaced with the typical usage of "bar" being a pointer to some location of the string constant in the program's object file mapped to memory. 然后,FOO的出现将被替换为“bar”的典型用法,“bar”是指向映射到存储器的程序的目标文件中的字符串常量的某个位置的指针。

There isn't really a difference. 没有什么区别。

No, 没有,

weeks = 12 * 4;

Is exactly the same as: 完全相同:

weeks = MONTHS * 4;

Does 12 take memory? 12记忆吗? No, Therefore neither does MONTHS . 不,因此MONTHS也没有。

if 12 is allocated depends of the usage and got nothing to do with the preprocessor at all 如果分配12取决于使用情况,并且与预处理器完全无关

Somefunction(MONTH);

would expand to 会扩大到

Somefunction(12);

12 here must ofc be stored somewhere, in this case it will be staticaly stored in the executable. 这里必须存储12,在这种情况下,它将被静态存储在可执行文件中。 The type will likely depend on what Somefunction take as an argument (or whatever the compiler decides fits best for this usage). 该类型可能取决于Somefunction作为参数的内容(或者编译器决定最适合此用法的内容)。 There is no know label to '12'. “12”没有已知的标签。

The preprocessoring is done before the actual compilation, as someone already wrote, it is like a automated texteditor. 预处理在实际编译之前完成,正如有人已经写过的那样,它就像一个自动化的texteditor。 So the compilation stage is fully unaware of what the preprocessor did. 因此编译阶段完全没有意识到预处理器的作用。

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

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