本文介绍了如果C标准说禁止使用空格,为什么clang和gcc都只在反斜杠后有空格时才发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When compiling the following snippet, both gcc and clang only issue a warning. Notice space after \ next to int:

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

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

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

gcc:

clang:

在5.1.1.2中的c99标准中,它表示:

In c99 standard in 5.1.1.2 it says:

为什么C编译器在这里不符合C标准?我认为这只是创作者的决定。我在gcc邮件列表中找到一条消息,认为是这种现象的引入:。他们说这样做是因为尾随空格很常见,并且他们不想将它们视为错误。

Why don't C compilers conform to C standard here? I think it's only their creators decision not to. I found a message on gcc mailing list that I think introduced this behavior: http://gcc.gnu.org/ml/gcc-patches/2000-09/msg00430.html. There, they say that this is done because trailing whitespaces are common and they don't want to treat them as an error. How common is that?

推荐答案

只要文件更改<$ c $,就允许编译器扩展语言。 c> gcc 在他们的文档中的。

The compiler is allowed to extend the language as long as the document the change which gcc does in their docs in section 6.21 Slightly Looser Rules for Escaped Newlines.

clang gcc 扩展名,并指向 gcc 文档:

and clang strives to support gcc extensions and points to the gcc docs on them:

因此,他们履行了对标准的义务。实际上,,您可以使用 -pedantic-errors 标志进行设置它是一个错误:

gcc also documents that you can use the -pedantic flag to generate a warning when using extensions and you can use -pedantic-errors flag to make it an error:

这篇关于如果C标准说禁止使用空格,为什么clang和gcc都只在反斜杠后有空格时才发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:20