为什么三字母在现代C ++编译器中会产生错误?

时间:2022-04-02 09:43:59

为什么三字母在现代C ++编译器中会产生错误?

Even in the GCC Compilers, the trigraphs are not getting compiled without explicitly specifying the trigraph attribute.

即使在GCC编译器中,如果没有明确指定trigraph属性,也不会编译三字符。

#include<stdio.h>

int main()
 {
 int a=4;
 if((a==4) ??! (a==5))
   printf("\nHello world!");
 return 0;
 }

This program saved as try.c gets compiled in GCC Compiler only when we specify gcc -Wall -trigraphs try.c and it still shows warnings. Can you enlist some compilers that will treat and handle trigraphs without any errors or warnings?

保存为try.c的程序仅在我们指定gcc -Wall -trigraphs try.c时才在GCC编译器中编译,它仍然显示警告。你可以招募一些能够处理和处理三字符而没有任何错误或警告的编译器吗?

4 个解决方案

#1


9  

Trigraphs were introduced by the 1989 ANSI C standard, and are retained in all later C standards. They also appear in the first ISO C++ standard, published in 1998, and in all later C++ standards up to C++14. (Trigraphs will be removed in C++17. Thanks to Jonathan Leffler and dyp for tracking down the details.)

Trigraphs由1989 ANSI C标准引入,并保留在所有后来的C标准中。它们也出现在1998年出版的第一个ISO C ++标准中,以及高达C ++ 14的所有后来的C ++标准中。 (Trigraphs将在C ++中删除17.感谢Jonathan Leffler和dyp追踪细节。)

They are not an optional feature in either language; all conforming compilers must support them and interpret them as specified by the respective language standard.

它们不是任何一种语言的可选功能;所有符合要求的编译器必须支持它们并按照相应的语言标准对其进行解释。

For example, if this program:

例如,如果这个程序:

#include <stdio.h>
int main(void) {
    if ('|' == '??!') {
        puts("ok");
    }
    else {
        puts("oops");
    }
    return 0;
}

prints oops, then your compiler is non-conforming.

打印oops,然后你的编译器不符合。

But many, perhaps most, C compilers are not fully conforming by default. As long as a compiler can be made to conform to the standard in some way, that's good enough as far as the standard is concerned. (gcc requires -pedantic and -std=... to do this.)

但许多(可能是大多数)C编译器默认情况下并不完全符合。只要编译器能够以某种方式符合标准,就标准而言,这就足够了。 (gcc需要-pedantic和-std = ...才能这样做。)

But even if a compiler is fully conforming, there's nothing in the standard that forbids a compiler from warning about anything it likes. A conforming C compiler must diagnose any violation of a syntax rule or constraint, but it can issue as many additional warnings as it likes -- and it needn't distinguish between required diagnostics and other warnings.

但即使编译器完全符合,标准中也没有任何内容禁止编译器警告它喜欢的任何内容。符合标准的C编译器必须诊断任何违反语法规则或约束的行为,但它可以发出任意数量的附加警告 - 并且无需区分所需的诊断和其他警告。

Trigraphs are very rarely used. The vast majority of development systems support directly all the characters for which trigraphs substitute: #, [, \, ], ^, {, |, }, ~.

Trigraphs很少使用。绝大多数开发系统直接支持三字符替换的所有字符:#,[,\,],^,{,|,},〜。

In fact, it's likely that trigraphs are used accidentally more often than they're used correctly:

实际上,有可能比使用正确使用三角戟更频繁地使用三角戟:

fprintf(stderr, "What just happened here??!\n");

Warning about trigraphs that might alter the meaning of a program (relative to the meaning it would have if the language didn't have trigraphs) is both permitted by the ISO standard and IMHO perfectly reasonable. Most compilers probably have options to turn off such warnings.

关于可能改变程序含义的三字母的警告(相对于语言没有三字母时的含义)是ISO标准和恕我直言都完全合理的。大多数编译器可能有选项来关闭此类警告。

Conversely, for a C++17 compiler that doesn't implement trigraphs, it would be reasonable to warn about sequences that would have been treated as trigraphs in C++14 or earlier. Again, an option to disable such warnings would be a good thing.

相反,对于不实现三字符的C ++ 17编译器,警告在C ++ 14或更早版本中被视为三字符的序列是合理的。同样,禁用此类警告的选项将是一件好事。

#2


5  

GCC is allergic to trigraphs. You have to explicitly enable them:

GCC对三卦过敏。您必须明确启用它们:

gcc -trigraphs ...

The GCC 4.7.1 manual says:

GCC 4.7.1手册说:

-trigraphs

Support ISO C trigraphs. The -ansi option (and -std options for strict ISO C conformance) implies -trigraphs.

支持ISO C三字符。 -ansi选项(以及严格ISO C一致性的-std选项)暗示-trigraphs。

It also says:

它还说:

-Wtrigraphs

Warn if any trigraphs are encountered that might change the meaning of the program (trigraphs within comments are not warned about). This warning is enabled by -Wall.

如果遇到任何可能改变程序含义的三字符,则发出警告(注释中的三字符不会被警告)。 -Wall启用此警告。

#3


2  

They might be turned off by default.

它们可能默认关闭。

"Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on"

“有些编译器支持关闭三元组识别的选项,或默认禁用三元组,并需要一个选项才能打开它们”

GCC might be one of the latter. Although it should by default ignore with warning, but in this case the ignoring might be causing the compile error

海湾合作委员会可能是后者之一。虽然默认情况下应该忽略警告,但在这种情况下忽略可能会导致编译错误

#4


1  

Trigraphs are converted at a very early stage of compilation, and can even be replaced in string literals. This makes errors that arise from trigraph translations very hard to detect (worst off if you consider debugging using a log and you find the output in your source).

Trigraph在编译的早期阶段进行转换,甚至可以用字符串文字替换。这使得由三字母翻译引起的错误很难检测到(如果您考虑使用日志进行调试并且在源中找到输出,则会出现最糟糕的情况)。

The warning you see will help you quickly spot a possible culprit to track the source of the bug. Basically it's warning you that something might not be as you think it is.

您看到的警告将帮助您快速发现可能的罪魁祸首,以跟踪错误的来源。基本上它会警告你,某些东西可能不像你想象的那样。

#1


9  

Trigraphs were introduced by the 1989 ANSI C standard, and are retained in all later C standards. They also appear in the first ISO C++ standard, published in 1998, and in all later C++ standards up to C++14. (Trigraphs will be removed in C++17. Thanks to Jonathan Leffler and dyp for tracking down the details.)

Trigraphs由1989 ANSI C标准引入,并保留在所有后来的C标准中。它们也出现在1998年出版的第一个ISO C ++标准中,以及高达C ++ 14的所有后来的C ++标准中。 (Trigraphs将在C ++中删除17.感谢Jonathan Leffler和dyp追踪细节。)

They are not an optional feature in either language; all conforming compilers must support them and interpret them as specified by the respective language standard.

它们不是任何一种语言的可选功能;所有符合要求的编译器必须支持它们并按照相应的语言标准对其进行解释。

For example, if this program:

例如,如果这个程序:

#include <stdio.h>
int main(void) {
    if ('|' == '??!') {
        puts("ok");
    }
    else {
        puts("oops");
    }
    return 0;
}

prints oops, then your compiler is non-conforming.

打印oops,然后你的编译器不符合。

But many, perhaps most, C compilers are not fully conforming by default. As long as a compiler can be made to conform to the standard in some way, that's good enough as far as the standard is concerned. (gcc requires -pedantic and -std=... to do this.)

但许多(可能是大多数)C编译器默认情况下并不完全符合。只要编译器能够以某种方式符合标准,就标准而言,这就足够了。 (gcc需要-pedantic和-std = ...才能这样做。)

But even if a compiler is fully conforming, there's nothing in the standard that forbids a compiler from warning about anything it likes. A conforming C compiler must diagnose any violation of a syntax rule or constraint, but it can issue as many additional warnings as it likes -- and it needn't distinguish between required diagnostics and other warnings.

但即使编译器完全符合,标准中也没有任何内容禁止编译器警告它喜欢的任何内容。符合标准的C编译器必须诊断任何违反语法规则或约束的行为,但它可以发出任意数量的附加警告 - 并且无需区分所需的诊断和其他警告。

Trigraphs are very rarely used. The vast majority of development systems support directly all the characters for which trigraphs substitute: #, [, \, ], ^, {, |, }, ~.

Trigraphs很少使用。绝大多数开发系统直接支持三字符替换的所有字符:#,[,\,],^,{,|,},〜。

In fact, it's likely that trigraphs are used accidentally more often than they're used correctly:

实际上,有可能比使用正确使用三角戟更频繁地使用三角戟:

fprintf(stderr, "What just happened here??!\n");

Warning about trigraphs that might alter the meaning of a program (relative to the meaning it would have if the language didn't have trigraphs) is both permitted by the ISO standard and IMHO perfectly reasonable. Most compilers probably have options to turn off such warnings.

关于可能改变程序含义的三字母的警告(相对于语言没有三字母时的含义)是ISO标准和恕我直言都完全合理的。大多数编译器可能有选项来关闭此类警告。

Conversely, for a C++17 compiler that doesn't implement trigraphs, it would be reasonable to warn about sequences that would have been treated as trigraphs in C++14 or earlier. Again, an option to disable such warnings would be a good thing.

相反,对于不实现三字符的C ++ 17编译器,警告在C ++ 14或更早版本中被视为三字符的序列是合理的。同样,禁用此类警告的选项将是一件好事。

#2


5  

GCC is allergic to trigraphs. You have to explicitly enable them:

GCC对三卦过敏。您必须明确启用它们:

gcc -trigraphs ...

The GCC 4.7.1 manual says:

GCC 4.7.1手册说:

-trigraphs

Support ISO C trigraphs. The -ansi option (and -std options for strict ISO C conformance) implies -trigraphs.

支持ISO C三字符。 -ansi选项(以及严格ISO C一致性的-std选项)暗示-trigraphs。

It also says:

它还说:

-Wtrigraphs

Warn if any trigraphs are encountered that might change the meaning of the program (trigraphs within comments are not warned about). This warning is enabled by -Wall.

如果遇到任何可能改变程序含义的三字符,则发出警告(注释中的三字符不会被警告)。 -Wall启用此警告。

#3


2  

They might be turned off by default.

它们可能默认关闭。

"Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on"

“有些编译器支持关闭三元组识别的选项,或默认禁用三元组,并需要一个选项才能打开它们”

GCC might be one of the latter. Although it should by default ignore with warning, but in this case the ignoring might be causing the compile error

海湾合作委员会可能是后者之一。虽然默认情况下应该忽略警告,但在这种情况下忽略可能会导致编译错误

#4


1  

Trigraphs are converted at a very early stage of compilation, and can even be replaced in string literals. This makes errors that arise from trigraph translations very hard to detect (worst off if you consider debugging using a log and you find the output in your source).

Trigraph在编译的早期阶段进行转换,甚至可以用字符串文字替换。这使得由三字母翻译引起的错误很难检测到(如果您考虑使用日志进行调试并且在源中找到输出,则会出现最糟糕的情况)。

The warning you see will help you quickly spot a possible culprit to track the source of the bug. Basically it's warning you that something might not be as you think it is.

您看到的警告将帮助您快速发现可能的罪魁祸首,以跟踪错误的来源。基本上它会警告你,某些东西可能不像你想象的那样。