The Art in Code

Snippets of famous, interesting, historically relevant or thought-provoking... code.


Website maintained by Filip Stanis Based on theme by mattgraham

003 - Trigraph

Code snippet of "??!" trigraph use

Snippet source

The code snippet above performs short-circuit evaluation: HandleError() will only happen if !ErrorHasOccured() evaluates to TRUE. However, instead of ||, which is normally used for this, the snippet above uses ??!??! which most C compilers still allow.

Explanation

Similar to spoken languages, some programming languages support digraphs and trigraphs - groups of two or three characters used to symbolise one character.

Before the current QWERTY keyboard layout was widespread, some keyboards didn’t have special characters such as |. To make things easier for programmers, the C preprocessor made is so that typing ??! is the same as typing |. Similar rules exist for other characters which were less common on keyboards at the time, such as #, ~ and ^.

While largely irrelevant nowadays, many C compilers still support this. This makes it possible to replace || with ??!??! anywhere in your C code.

Needless to say, using ??!??! instead of || in code is both unnecessary and something that few are familiar with, making this a bad practice for production code.