Posted on: 08/09/2009

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 legit 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 defaultly disabled on gcc (g++) and you can enable them by using –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 occurences of trigraphs inside strings get replaced. Be very careful when using two consecutive question marks!


7 Responses to "Trigraphs"

This one seems excessively silly. Why would they still be enabled by default anywhere?
It’s even worse when you consider that if your advice would have been ‘turn off trigraphs in vs2008’, then people would have been bitten when porting, or just copying the code somewhere.

Also, it seems disabling them in vs2008 is not trivial (they are not in the preprocessor list of config options). Stupidity at work I guess.

I couldn’t agree more.

However, I think that disabling this would cause virtually no porting problems since I really doubt that there’s a significant amount of code using these “features” – and if there is, it’s pretty easy to find (very small number of sequences to search & replace if needed). So here’s a call to Microsoft to disable this in the next Visual Studio release.

I’m not sure you got my meaning.
Let’s say that I have a piece of code that has trigraphs in it. Maybe because I did so ahead of time, or maybe because I found out about it, I cancel trigraphs in vs.
Later on, someone else uses my code, but in a new project. He will get bitten by trigraphs as well.

Of course, it will happen to him the same way if he ports code from gcc.

You are considering this from the side of someone who decides not to use this option by disabling it in his compiler, while i was referring to the case where someone actually wanted to use it.
In your scenario I would say that anybody that is aware of this feature should not rely on it being disabled – any such programmer must be careful when programming. For example, escape _every_ second consecutive question mark (“?\?!” etc). Thats why we have an escape sequence for the question mark.

Hmm very strange….
But nice to know…


I m not able to compile this code in vs 2008.
How to enable trigraphs in vs2008?

The sad thing is that Trigraphs are enabled in VS2008 by default.
I have just checked the code under VS2008 and it works as expected. The (whole) blog has been altered to use the [/sourcecode] tags, so copy&paste should go alot smoother now. Let me know if you have any more issues.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 27 other followers


%d bloggers like this: