C Language | Introduction to C | Trigraphs

C supports three-character sequences beginning with ?? that are replaced with predefined symbols.

Character Replacement

Although not widely known, C supports three-character sequences called trigraphs. They are related to the ISO 646 standard. ISO 646 allowed certain ASCII characters to be changed so that national standards could represent symbols required by languages other than English. However, this meant that the same character code could display different characters.

Trigraphs were introduced to represent ASCII characters that could not be entered in some languages or on some keyboards. A trigraph consists of two question marks (??) followed by another symbol. They are almost never used today, but text containing ?? in a string literal could unexpectedly be converted into another character.

The following three-character sequences are trigraphs. Each sequence is replaced with the corresponding single character during compilation.

Table 1 - Trigraphs

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

This conversion happens first during compilation. More precisely, a compiler analyzes source programs through a defined sequence of translation phases. Trigraphs are processed before lines and tokens are identified.

Code 1

??=include <stdio.h>

int main() ??<
  printf("??= , ??( , ??)??/n");
  return 0;
??>

This C source program is valid. Because trigraphs are converted before anything else, the compiler sees the correct symbols by the time it analyzes tokens.

To write ?? inside a string literal, use the escape sequence \? \?.

Because trigraphs are rarely used, many mainstream compilers either do not implement them or disable them by default to improve compilation speed. For example, Borland C++ Compiler 5.5 does not implement trigraphs in the compiler. Instead, it provides the TRIGRAPH.EXE conversion tool.

>trigraph sourceFileName

This tool replaces trigraphs in a source file with the corresponding characters. To compile source code containing trigraphs with Borland C++ Compiler 5.5, run this tool first.

In Microsoft Visual C++, trigraphs are disabled by default. Enable them with the /Zc:trigraphs compiler option. The editor does not support entering code with trigraphs.