预处理器指令应该位于行的开头吗?

发布于 2024-10-12 12:52:07 字数 624 浏览 4 评论 0原文

不久前,我发现了一个(相当古老的)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦醒灬来后我 2024-10-19 12:52:07

一些旧的 C 编译器要求 #define (例如)与左边距齐平:

#define FOO bar

其他 C 编译器只要求 # 位于左边距,因此您可以:

#    define FOO bar

较新的 C 编译器倾向于接受任何前导空格后的 #

    #define FOO bar

如果您希望与此类较旧的编译器兼容,您至少应该将 # 放在第一列中。如果兼容性并不重要,那么就取决于你了。

我通常会尝试不要在函数中嵌入#ifdef 块,因此它们是否应该缩进的整个问题基本上就消失了。

Some old C compilers required that the #define (for example) be flush with the left margin:

#define FOO bar

Other C compilers required only that the # be at the left margin, so you could:

#    define FOO bar

Newer C compilers tend to accept the # after any leading whitespace:

    #define FOO bar

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.

帅的被狗咬 2024-10-19 12:52:07

来自 gcc C 预处理器文档

预处理指令是以下行
您的程序以 #' 开头。#' 前后允许有空格。

from gcc C preprocessor documentation:

Preprocessing directives are lines in
your program that start with #'. Whitespace is allowed before and after the#'.

走过海棠暮 2024-10-19 12:52:07

不,它们不需要位于行的开头,但前面只能有空格(空格、制表符……)。

通常它们被放在行的开头,因为它们不受其所属范围的限制,因为它们在实际的 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.

多像笑话 2024-10-19 12:52:07

没关系。这样看,如果代码未识别并且在 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

回首观望 2024-10-19 12:52:07

我不认为编译器“关心”预处理之前是否有空格 - 它应该是相同的......

I don't think the compiler "cares" if you have spaces before the preprocessed - it should be the same...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文