Home Blog Index
Joseph Petitti —

The C ??!??! operator

The C programming language has a bit of a reputation for having weird syntax. The #define directive, pointer declaration, and uncommon array indexing methods can all make it hard to understand what's going on in a C program. Still, some of C's weirdness manages to surprise me even after using it for a few years. For example, what do you think this expression does?

!x ??!??! y();

If you guessed that it's a basic if statement, you'd be correct. The above line is equivalent to this:

if (x) { y(); }

The first step to understanding this is to realize that ??!??! isn't really an operator at all, it's actually two trigraphs in a row. This is an old feature of the C preprocessor that replaces a sets of three characters with their equivalent before compiling.

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

These were apparently included so that programmers who didn't have certain keys on their keyboard could still use all the features of C.

We can see that the ??! gets replaced by the pipe character, so ??!??! is actually || the logical OR operator.

Thanks to the power of short-circuit evaluation, y() is only executed if the first statement, !x is false.

This is not a good way of writing an if statement, and I would never recommend using this in practice, but it's interesting to see how rules of C can lead to such weird-looking code being perfectly valid.

Bonus

As another weird C thing, here's a perfectly valid way to access an element of an array:

NULL[array + 10];

Due to the nature of memory addressing, this is the same as writing:

array[10];

To make it even cooler, try:

0??(array + 10??);