c - 严格符合 ANSI C 的 ON/OFF 模式的不同结果

标签 c gcc ansi-c trigraphs

为什么严格遵守ANSI C程序的ONOFF模式会产生不同的结果?遵守严格编写的原因是大多数现代工业编译器默认对自己的语言进行一些扩展,有的默认是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 standard5.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/

相关文章:

linux - 我可以在 Debian/Linux 上安装两个版本的 GCC 吗?

复杂的通用堆栈

c - 包含返回函数的宏

c - typedef 指针常量怪异

c++ - 可以在 C/C++ 中声明但不定义局部变量吗?

file-io - 如何倒序写入文件

c - 如何从非浏览器设备发出 HTTP POST?

c - 从环境 C 中的给定变量中删除值

c - c程序中的段错误

pjsip ld 中的 gcc 编译错误返回 1 退出状态