为什么严格遵守ANSI C
程序的ON
或OFF
模式会产生不同的结果?遵守严格编写的原因是大多数现代工业编译器默认对自己的语言进行一些扩展,有的默认是C99等。
#include <stdio.h>
#include <string.h>
int main (void)
{
int len;
len = strlen ("??=");
printf ("len=%d\n", len);
return 0;
}
这是结果。在这两种情况下都提交了编译器选项 -w
来抑制警告:
$ gcc t.c -w
$ ./a.out
len=3
$ gcc t.c -ansi -w
$ ./a.out
len=1
最佳答案
当它不处于 ansi 模式时 gcc
不会将 trigrpah ??=
(length 3) 转换为 #
(length 1) 解释了长度差异。
如果您没有抑制警告,gcc
会警告它忽略 trigraph在非 ansi 模式下( see it live ):
warning: trigraph ??= ignored, use -trigraphs to enable [-Wtrigraphs]
您可以在Options Controlling C Dialect中阅读有关方言选项的更多信息。 gcc
文档的部分。 -ansi
的内容如下:
[...] It also enables the undesirable and rarely used ISO trigraph feature. [...]
三字母包含在 draft C99 standard 中5.2.1.1
三字母序列部分内容如下:
Before any other processing takes place, each occurrence of one of the following sequences of three characters (called trigraph sequences12)) is replaced with the corresponding single character.
以下序列包括:
??= #
关于c - 严格符合 ANSI C 的 ON/OFF 模式的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918847/