三字符组Trigraphs

C 源程序的源字符集包含在 7 位 ASCII 字符集中,但它是 ISO 646-1983 固定语言代码集的超集。The source character set of C source programs is contained within the 7-bit ASCII character set but is a superset of the ISO 646-1983 Invariant Code Set. 三元组序列仅允许使用 ISO(国际标准组织)固定语言代码集编写 C 程序。Trigraph sequences allow C programs to be written using only the ISO (International Standards Organization) Invariant Code Set. 三元组是三字符序列(由两个连续问号引入),编译器会将它替换为其相应的标点字符。Trigraphs are sequences of three characters (introduced by two consecutive question marks) that the compiler replaces with their corresponding punctuation characters. 可以将 C 源文件中的三元组与不包含某些标点字符的方便图形表示形式的字符集一起使用。You can use trigraphs in C source files with a character set that does not contain convenient graphic representations for some punctuation characters.

C++ 17 从语言中删除三字符组。C++17 removes trigraphs from the language. 实现可能会继续支持三元组作为从物理源文件到基本源字符集的实现定义的映射的一部分,但标准不鼓励实现这样做。Implementations may continue to support trigraphs as part of the implementation-defined mapping from the physical source file to the basic source character set, though the standard encourages implementations not to do so. 通过 C++ 14,三元组受到如在 C 中一样的支持。Through C++14, trigraphs are supported as in C.

Visual C++ 继续支持三元组替换,但默认处于禁用状态。Visual C++ continues to support trigraph substitution, but it's disabled by default. 有关如何启用三元组替换的信息,请参阅 /Zc: trigraphs(Trigraphs 替换)For information on how to enable trigraph substitution, see /Zc:trigraphs (Trigraphs Substitution).

下表显示了九个三元组序列。The following table shows the nine trigraph sequences. 第一列中的标点字符的源文件中的所有匹配项都将替换为第二列中的相应字符。All occurrences in a source file of the punctuation characters in the first column are replaced with the corresponding character in the second column.

三元组序列Trigraph Sequences

三元组Trigraph 标点字符Punctuation Character
??=??= #
??(??( [[
??/??/ \
??)??) ]]
??'??' ^
??<??< {{
??!??! ||
??>??> }}
??-??- ~

三元组始终被视为单个源字符。A trigraph is always treated as a single source character. 在识别字符串和字符常数中的转义符前,三元组的转换会在第一个转换阶段出现。The translation of trigraphs takes place in the first translation phase, before the recognition of escape characters in string literals and character constants. 仅识别上表中显示的九个三元组。Only the nine trigraphs shown in the above table are recognized. 所有其他字符序列都未转换。All other character sequences are left untranslated.

字符转义序列 \? 可防止对类似于三元组的字符序列进行错误解释。The character escape sequence, \?, prevents the misinterpretation of trigraph-like character sequences. (有关转义序列的信息,请参阅转义序列。)例如,如果您尝试打印具有 What??! 语句的字符串 printf(For information about escape sequences, see Escape Sequences.) For example, if you attempt to print the string What??! with this printf statement

printf( "What??!\n" );  

打印的字符串为 What|,因为 ??! 是一个三元组序列,它将替换为 | 字符。the string printed is What| because ??! is a trigraph sequence that is replaced with the | character. 编写如下语句以正确打印字符串:Write the statement as follows to correctly print the string:

printf( "What?\?!\n" );  

在此 printf 语句中,第二个问号的前面的反斜杠转义符可防止将 ??! 错误解释为三元组。In this printf statement, a backslash escape character in front of the second question mark prevents the misinterpretation of ??! as a trigraph.

另请参阅See Also

/Zc: trigraphs (Trigraphs 替换) /Zc:trigraphs (Trigraphs Substitution)
C 标识符C Identifiers