Saturday 17 December 2016

What does the C ??!??! operator do?

As already stated ??!??! is essentially two trigraphs (??! and ??! again) mushed together that get replaced-translated to ||, i.e the logical OR, by the preprocessor.


The following table containing every trigraph should help disambiguate alternate trigraph combinations:


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

Source: C: A Reference Manual 5th Edition


So a trigraph that looks like ??(??) will eventually map to [], ??(??)??(??) will get replaced by [][] and so on, you get the idea.


Since trigraphs are substituted during preprocessing you could use cpp to get a view of the output yourself, using a silly trigr.c program:


void main(){ const char *s = "??!??!"; }

and processing it with:


cpp -trigraphs trigr.c

You'll get a console output of


void main(){ const char *s = "||"; }

As you can notice, the option -trigraphs must be specified or else cpp will issue a warning; this indicates how trigraphs are a thing of the past and of no modern value other than confusing people who might bump into them.




As for the rationale behind the introduction of trigraphs, it is better understood when looking at the history section of ISO/IEC 646:



ISO/IEC 646 and its predecessor ASCII (ANSI X3.4) largely endorsed existing practice regarding character encodings in the telecommunications industry.


As ASCII did not provide a number of characters needed for languages other than English, a number of national variants were made that substituted some less-used characters with needed ones.



(emphasis mine)


So, in essence, some needed characters (those for which a trigraph exists) were replaced in certain national variants. This leads to the alternate representation using trigraphs comprised of characters that other variants still had around.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...