多行 DEFINE 指令?
我不是专家,所以请放轻松.这两个代码段有什么区别吗?
I am not an expert, so go easy on me. Are there any differences between these two code segments?
#define BIT3 (0x1
<
<
3)
static int a;
和
#define BIT3 (0x1 << 3) static int a;
另外,有没有办法在一行中写第一个?这种多线样式的意义何在?下面的代码好吗?
Also, is there a way to write the first in one line? What is the point of this multi-line style? Is the following code good?
#define BIT3 (0x1 << 3)
static int a;
推荐答案
如果您有一个非常复杂的宏,如果它全部在一行上将难以阅读(尽管不建议使用多行宏)非常复杂的宏).
A multi-line macro is useful if you have a very complex macro which would be difficult to read if it were all on one line (although it's inadvisable to have very complex macros).
一般来说,您可以使用续行字符 编写多行定义.所以例如
In general, you can write a multi-line define using the line-continuation character, . So e.g.
#define MY_MACRO printf(
"I like %d types of cheese
",
5
)
但是你不能在你的第一个例子中做到这一点.你不能像那样拆分令牌;<<
左移运算符必须始终在没有任何分隔空格的情况下编写,否则它将被解释为两个小于运算符.所以也许:
But you cannot do that with your first example. You cannot split tokens like that; the <<
left-shift operator must always be written without any separating whitespace, otherwise it would be interpreted as two less-than operators. So maybe:
#define BIT3 (0x1
<<
3)
static int a;
现在相当于你的第二个例子.
which is now equivalent to your second example.
[虽然我不确定那个宏会有什么用处!]
相关文章