多行DEFINE指令? [英] Multi-line DEFINE directives?

查看:142
本文介绍了多行DEFINE指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专家,这样下去容易对我。在这两个code段之间有什么区别?

 的#define BIT3(为0x1
<
<
3)
静态int类型的;

 的#define BIT3(为0x1< 3;)静态int类型的;

此外,有没有先写在一行中的方法吗?什么是这种多线样式的地步?在下面的code好?​​

 的#define BIT3(为0x1< 3;)
静态int类型的;


解决方案

如果你有一个非常复杂的宏这将是难以阅读,如果它是全部在一行上(虽然它不可取的多行宏是有用的非常复杂的宏)

在一般情况下,你可以写使用行继续符, \\ 多行定义。因此,例如

 的#define MY_MACRO的printf(\\
    我喜欢%D型奶酪的\\ n,\\
    5 \\
    )

但你不能这样做,你的第一个例子。你可以不拆这样的标记;在<< 左移位运算符必须始终没有任何空格分开写,否则会间preTED为两个低于运营商。因此,也许:

 的#define BIT3(为0x1 \\
    << \\
    3)\\
    静态int类型的;

这是现在相当于你的第二个例子。

[虽然我不知道如何宏将永远是有用的!]

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;

and

#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\n", \
    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.

[Although I'm not sure how that macro would ever be useful!]

这篇关于多行DEFINE指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆