Multi-line DEFINE directives?(多行 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.
[虽然我不确定那个宏会有什么用处!]
这篇关于多行 DEFINE 指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多行 DEFINE 指令?


基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01