How would you like the next piece of code?
??=include <iostream> ??=define A "hey" int main () ??< char x??(??) = A; if (0 ??' 1) std::cout << x << std::endl; // what do you say about this code??/ std::cout << "amusing" << std::endl; ??>
This is actually a legitimate sourcecode. It generates a single output line of “hey”.
This code uses a C++ feature called trigraphs, documented here. The trigraphs are present in the language for reasons that were (possibly) more common in the past; If you didn’t have all the characters on the available keyboard, you could use these trigraphs to represent the characters you were lacking. I believe this issue is not a major problem nowadays.
The real crazy thing about this option is that, it’s enabled in Visual Studio (2008) by default! I’m sure that if I wrote the following line
std::cout << "testing123??!" << std::endl;
and got the output below
I wouldn’t know what hit me.
I’d like to add that trigraphs are disabled in gcc (and g++) by default, but can be enabled by using the –trigraphs switch, which makes far more sense than having them enabled by default.
One extra important piece of information regarding this issue is that, according to Wikipedia, trigraphs are the first thing that gets replaced by the preprocessor – before any other processing is done. This property can be observed in the example, where even occurrences of trigraphs within strings got substituted. Be very careful when using two consecutive question marks!