C 语言 and,C中慎用两个连续'?',其它语言也有类似的digraphs and trigraphs

本文介绍了C语言中的 trigraph 和 digraph,它们是预处理器处理的一部分。trigraphs 如 ??/ 可用于表示特殊字符,而 digraphs 如 <: 作为 trigraphs 的更易读替代,在 tokenization 阶段进行替换。文章讨论了如何在字符串和注释中使用这些序列,以及它们在源代码中的潜在影响和陷阱。C99 标准引入了 digraphs 以提供更清晰的语法选项。
摘要由CSDN通过智能技术生成

http://en.wikipedia.org/wiki/C_trigraph#C

C

C programming language

supports digraphs in ISO C 94 mode of compiling.

The C preprocessor replaces all occurrences of the

following nine trigraph sequences by their single-character

equivalents before any other processing.

A programmer may want to place two question marks together yet

not have the compiler treat them as introducing a trigraph. The C

grammar does not permit two consecutive ? tokens, so

the only places in a C file where two question marks in a row may

be used are in multi-character constants, string literals, and

comments. To safely place two consecutive question marks within a

string literal, the programmer can use string concatenation

"...?""?..." or an escape sequence

"...?\?...".

Trigraph

Equivalent

??=

#

??/

\

??'

^

??(

[

??)

]

??!

|

??<

{

??>

}

??-

~

??? is not itself a trigraph sequence.

The ??/ trigraph can be used to introduce an

escaped newline for line splicing; this must be taken into account

for correct and efficient handling of trigraphs within the

preprocessor. It can also cause surprises, particularly within

comments. For example:

// Will the next line be executed????????????????/

a++;

which is a single logical comment line (used in C++ and C99),

and

/??/

* A comment *??/

/

which is a correctly formed block comment.

In 1994 a normative amendment to the C standard, included in

C99,

supplied digraphs as more readable alternatives to five of the

trigraphs. They are:

Digraph

Equivalent

<:>

[

:>

]

{

%>

}

%:

#

Unlike trigraphs, digraphs are handled during tokenization, and any digraph must always

represent a full token by itself, or compose the token

%:%: replacing the preprocessor concatenation token

##. If a digraph sequence occurs inside another token,

for example a quoted string, or a character constant, it will not

be replaced.

Notes

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值