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.
The C preprocessor replaces all occurrences of the following nine trigraph sequences by their single-character equivalents before any other processing.
??? is not itself a trigraph sequence, but when followed by a character such as
- it will be interpreted as
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.
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.
As a note,
%:%: is treated as a single token, rather than two occurrences of %:.