Hub Nexus
更新

作者暂无作者认领

发现可以改进的地方?提出一个改动吧。

赞助

C was designed in English and assumes the common ASCII character set made for English, which includes such characters as {, }, , ], and so on. Some other character sets (like [EBCDIC on mainframes), however, do not have these or other characters which are required by C. Since even the simplest C program uses curly braces to define main(), developing in C would be impossible on these systems.

To solve this problem, trigraph sequences can be substituted for the symbols.[1] They will work in any situation; the first translation phase of compilation replaces the trigraph sequences with their corresponding single-character equivalents.[2]

Trigraphs were later supplemented with digraphs,[3] but neither were in widespread use except in mainframe systems. Trigraphs have been removed from C in version C23.[4] However, they are described here as they may appear in some legacy systems' codebases.

Trigraphs

The following trigraph sequences exist, and no other. Each question mark ? that does not begin one of the trigraph sequences listed is not changed.

SequenceReplacement
??=#
??([
??/\
??)]
??'^
??<{
??!\
??>}
??-~

The effect of this is that source code such as

??=include <stdio.h>

int main(int argc, char **argv) ??<
char message??(20??) = "??-??-Hello, world.??-??-";
printf("%s??/n", message);
??>

will, after trigraphs are replaced, be the equivalent of

#include

int main(int argc, char **argv) {
char message[20] = "Hello, world.";
printf("%s\n", message);
}

Should the programmer want a trigraph not to be replaced, within strings and character constants (which is the only place they would need replacing and it would change things), the programmer can simply escape the second question mark; e.g.

printf ("Two question marks in a row: ??!\n");

MSVC and GCC do not process trigraphs by default, needing the /Zc:trigraphs or -trigraphs (respectively) flag to be passed on the command line to the compiler.[5][6] Clang processes them by default unless the C standard is set to a GNU mode.[7]

Digraphs

Digraphs are equivalent to the following tokens except for their spelling:

DigraphEquivalent
<:[
:>]
<%{
%>}
%:#
%:%:##

In other words, they behave differently when stringized as part of a macro replacement, but are otherwise equivalent.

iso646.h

Header iso646.h focuses only on solving the symbol problem for operators, but takes a different approach of defining keyword-like macros to stand in for them. It is named after the ISO/IEC 646 character encoding standard, which has some variants that do not have these symbols; some variants also must rely on trigraphs or digraphs.

Macros

The iso646.h header defines the following 11 macros as stated below:

MacroDefined as
and&&
and_eq&=
bitand&
bitor\
compl~
not!
not_eq!=
or\\
or_eq\=
xor^
xor_eq^=

References

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

收录于这些专栏C programming

语言English

许可协议: CC BY-SA 4.0 · 改编自 en.wikibooks.org

1

0

0

0

Spinner Logo

留言

Spinner Logo
版本: 2CC0 1.0 — public domain
The runaway star that left the Tarantula Nebula
版本: 2CC0 1.0 — public domain
The Blackwell School, where segregation had no law behind it
版本: 2CC0 1.0 — public domain
The Eagle Nebula, seen in the infrared
版本: 2CC0 1.0 — public domain
The house where the Equal Rights Amendment was written
版本: 2CC0 1.0 — public domain
The Aleutians, the forgotten front of the Second World War
版本: 2CC0 1.0 — public domain
The Cosmic Cliffs are not cliffs