This project has moved and is read-only. For the latest updates, please go here.

What is Digraphs and Trigraphs?

In computer programming, digraphs and trigraphs are sequences of two and three characters, respectively, that appear in source code and, according to a programming language specification, should be treated as if they were single characters.

Why Digraphs and Trigraphs exist?

Various reasons exist for using digraphs and trigraphs: keyboards may not have keys to cover the entire character set of the language, input of special characters may be difficult, text editors may reserve some characters for special use and so on.

Trigraphs - The C preprocessor replaces all occurrences of the following nine trigraph sequences by their single-character equivalents before any other processing.

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

??? is not itself a trigraph sequence, but when followed by a character such as - it will be interpreted as ? + ??-

Digraphs - 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.

Digraph Equivalent
<: [
:> ]
<% {
%> }
%: #


C++ introduces tokens. The C++ Standard makes this comment with regards to the term "digraph":

The term "digraph" (token consisting of two characters) is not perfectly descriptive, since one of the alternative preprocessing-tokens is %:%: and of course several primary tokens contain two characters. Nonetheless, those alternative tokens that aren’t lexical keywords are colloquially known as "digraphs".

Token Equivalent
%:%: ##
compl ~
not !
bitand &
bitor |
and &&
or ||
xor ^
and_eq &=
or_eq |=
xor_eq ^=
not_eq !=

As a note, %:%: is treated as a single token, rather than two occurrences of %:.

Read C/C++ Interview Question

Last edited Dec 22, 2015 at 7:20 AM by vineetchoudhary, version 2