c - 如果 C 标准说禁止使用空格,为什么 clang 和 gcc 只在反斜杠后有空格时发出警告?

标签 c gcc clang standards backslash

编译以下代码片段时,gcc 和 clang 都只发出警告。注意 int 旁边的 \ 之后的空格:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int \ 
        a = 10;

    printf("%d\n", a);
}

海湾合作委员会:

main.c:7:6: warning: backslash and newline separated by space [enabled by default]

clang :

main.c:7:7: warning: backslash and newline separated by space int ^

在 5.1.1.2 的 c99 标准中它说:

Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.

为什么C编译器不符合这里的C标准?我认为这只是他们的创作者决定不这样做。我在 gcc 邮件列表上发现了一条消息,我认为它引入了这种行为:http://gcc.gnu.org/ml/gcc-patches/2000-09/msg00430.html .在那里,他们说这样做是因为尾随空格很常见,他们不想将它们视为错误。这有多普遍?

最佳答案

编译器被允许扩展语言,只要记录 gcc 在其文档中的 6.21 Slightly Looser Rules for Escaped Newlines 部分所做的更改。 .

Recently, the preprocessor has relaxed its treatment of escaped newlines. Previously, the newline had to immediately follow a backslash. The current implementation allows whitespace in the form of spaces, horizontal and vertical tabs, and form feeds between the backslash and the subsequent newline. The preprocessor issues a warning, but treats it as a valid escaped newline and combines the two lines to form a single logical line. This works within comments and tokens, as well as between tokens. Comments are not treated as whitespace for the purposes of this relaxation, since they have not yet been replaced with spaces.

clang strives to support gcc 扩展并指向关于它们的 gcc 文档:

this document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.

因此,他们履行了与标准相关的义务。事实上Linux depends on many gcc extensions .我们可以通过查看 C99 标准草案部分 4. Conformance 段落 6 看到这一点,其中说:

[...]A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.3)

脚注 3 说:

This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.

和第 8 段:

An implementation shall be accompanied by a document that defines all implementation defined and locale-specific characteristics and all extensions.

gcc 还记录了您可以使用 -pedantic 标志来 generate a warning when using extensions您可以使用 -pedantic-errors 标志使其成为错误:

[...] to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

关于c - 如果 C 标准说禁止使用空格,为什么 clang 和 gcc 只在反斜杠后有空格时发出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21635063/

相关文章:

c - _dl_sysinfo_int80 的用途是什么?

c - 这种用于堆栈切换的内联汇编方法可以吗?

c++ - 交叉编译 Linux 3.15.3 for mips(el) vu solo2 Vlan 模块支持 (8021q)

c - _Generic() 宏不扩展

c - 编译 C 库时的警告

c - *、&、...解释指针、变量和地址的关系和命名,使用以下代码块:

c - OpenMP 并行区域的不同运行时间

c - 如何在 C 中将 char* 转换为 char[]

linker - 有没有办法确保 clang 链接未引用的库符号

c - 使用 C 从 .raw 文件恢复 JPEG 图像