5

I was going through few interview questions and I came across example as below. I tried the example for simple input/output and also for some logic and it works without any problems.

??=include <stdio.h>

int main(void) 
??<
    printf("Hello");
    // Other code lines here
    return 0;
??>

To my surprise, this worked without any compilation issue and output was as required.

What is the significance of '??=', '??<' and '??>' here ?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jasmeet
  • 1,315
  • 11
  • 23
  • @user3121023 Thanks for the comment. I got the confusion cleared after I searched for trigraphs. – Jasmeet Jul 28 '20 at 11:16
  • Trigraphs are provided to allow a minimal character set. Aside: early keyboards had a `#` key or a `£` key but not both, which I guess led to some people referring to the `#` hash symbol as a "pound". Happy that it is reverting as in "hash-tag" not "pound-tag". – Weather Vane Jul 28 '20 at 11:27
  • 1
    @WeatherVane Yeah I just read these were used by old keyboards that did not contain complete key set used by C/C++. Not sure why this ended up in examples for interview. Such kind of keyboards can rarely be found now. – Jasmeet Jul 28 '20 at 11:35
  • I've never seen trigraphs used in the wild. Usually, programmers would just use whatever character glyphs their local 7-bit character set mapped to the corresponding ASCII codes, for example replacing `#` with `£` in the UK, or replacing `\ ` with `¥` in Japan. – Ian Abbott Jul 28 '20 at 12:25
  • @WeatherVane "pound sign" is because the [# character is literally used to represent "pounds as a unit of weight or mass"](https://www.lexico.com/definition/pound_sign) – Andrew Henle Jul 28 '20 at 13:02
  • @AndrewHenle oh, I didn't know that US usage. I seem to remember an old printer which had a switch setting to determine what was printed for `0x23`, because those keyboards didn't use a different code for `#` and `£`. It was just the key-top. – Weather Vane Jul 28 '20 at 13:05
  • more duplicates: [What does the ??!??! operator do in C?](https://stackoverflow.com/q/7825055/995714), [Escape sequence for ? in c++](https://stackoverflow.com/q/1586834/995714), [Cryptic line “??!??!” in legacy code](https://stackoverflow.com/q/17718286/995714), [What is the meaning of these strange question marks?](https://stackoverflow.com/q/23825603/995714), [What is the meaning of `???-` in C++ code?](https://stackoverflow.com/q/16662276/995714) – phuclv Jul 28 '20 at 13:18

1 Answers1

4

What is the significance of '??=', '??<' and '??>' here ?

??= will be replaced with #,

??< will be replaced with {,

??> will be replaced with },

by the preprocessor. These are called trigraphs. There are 9 trigraphs in total; the others are:

??( will be replaced with [,

??) will be replaced with ],

??/ will be replaced with \,

??' will be replaced with ^,

??! will be replaced with |,

??- will be replaced with ~.

Trigraphs are processed very early in the translation process, before the source code is tokenized. They can affect comments and strings and character literals.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278