预处理器指令应该位于行的开头吗?
不久前,我发现了一个(相当古老的)C 编译器,它以这种方式扫描宏(伪代码):
if line.startswith("#include") or line.startswith("#define"):
...
.. 这给我提出了一个问题:宏应该真正放置在哪里,在一行的开头,就像这样:
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
#if defined(__GNUC__)
...
#else
...
#endif
#endif
}
或者更确切地说,像这样(因为这就是我这样做的方式,为了提高可读性):
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
# if defined(__GNUC__)
...
# else
...
# endif
#endif
}
缩进预处理器代码的方式是否标准化,也就是说,无论我如何缩进它,它总是以相同的方式工作?
A while ago I have discovered an (rather ancient) C Compiler, which scanned macros this way (Pseudo code):
if line.startswith("#include") or line.startswith("#define"):
...
.. Which kind of raised the question for me where macros should really be placed, at the beginning of a line, like so:
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
#if defined(__GNUC__)
...
#else
...
#endif
#endif
}
Or rather like so (as that's the way I do it, for improved readability):
void stuff()
{
#if defined(WIN32) || defined(_WIN32)
...
#else
# if defined(__GNUC__)
...
# else
...
# endif
#endif
}
Is the way one indents the Preprocessor code standardized, that is, no matter how i indent it, it will always work the same way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一些旧的 C 编译器要求
#define
(例如)与左边距齐平:其他 C 编译器只要求
#
位于左边距,因此您可以:较新的 C 编译器倾向于接受任何前导空格后的
#
:如果您希望与此类较旧的编译器兼容,您至少应该将
#
放在第一列中。如果兼容性并不重要,那么就取决于你了。我通常会尝试不要在函数中嵌入#ifdef 块,因此它们是否应该缩进的整个问题基本上就消失了。
Some old C compilers required that the
#define
(for example) be flush with the left margin:Other C compilers required only that the
#
be at the left margin, so you could:Newer C compilers tend to accept the
#
after any leading whitespace:If you want compatibility with such older compilers, you should at least put your
#
in the first column. If compatibility doesn't matter, then it's up to you.I would usually try not to embed
#ifdef
blocks inside functions, so the whole question of whether they should be indented mostly goes away.来自 gcc C 预处理器文档:
from gcc C preprocessor documentation:
不,它们不需要位于行的开头,但前面只能有空格(空格、制表符……)。
通常它们被放在行的开头,因为它们不受其所属范围的限制,因为它们在实际的 C 代码之前进行了预处理。
No, they don't need to be at the beginning of the line, but they can only have blanks (spaces, tabs, ...) before them.
Usually they're put at the beginning of the line because they're not subjected to the scopes they're into, since they're preprocessed before actual C code.
没关系。这样看,如果代码未识别并且在 1 行中它仍然应该编译(只有代码、预处理器/包含其他一些东西需要单独的行)。
编辑:似乎真正老的编译器对此很挑剔。预处理器应该位于一行,就像其他非代码事物(例如包含)一样
Doesn't matter. See it this way, if code was not idented and in 1 line it still should compile(only code, preprocessor/includes at some other things needs a seperate line).
Edit: seems to be that really old compiler are picky about this. Preprocessor should be at one line, just like other non-code things like includes
我不认为编译器“关心”预处理之前是否有空格 - 它应该是相同的......
I don't think the compiler "cares" if you have spaces before the preprocessed - it should be the same...