Search code examples
c++visual-studio-2008string-literalscharacter-replacement

Visual Studio 2008: String Literals "??-", "??'", "??=" corrupt


Recently I came across a bug in Visual Studio 2008 (at least I think it is one).

When I try to create string-literals with two questionmarks followed by another character, something weird occurs: Those three chars are replaced by another char.

Examples:

printf("??-"); --> ~  (hyphen)
printf("??'"); --> ^  (circumflex)
printf("??="); --> #  (hash)
printf("??)"); --> ]  (square braket)
printf("??("); --> [  (square braket)
printf("??/"); --> \  (backslash)
printf("??!"); --> |  (pipe)
printf("??%"); --> ?? (percent sign disappears)

Does anybody know the reason for this replacement?


Solution

  • It's no bug, more a hangover of history. They're C trigraphs - https://msdn.microsoft.com/en-us/library/bt0y4awe.aspx

    Di/Tri-graphs are a way to represent characters that weren't easily available back when the language was developed. They let you enter symbols into your source code that you may not have a keyboard key to represent.

    Tri-graph Symbol
    ??=       #
    ??/       \
    ??'       ^
    ??(       [
    ??)       ]
    ??!       |
    ??<       {
    ??>       }
    ??-       ~
    

    The % issue is just the usual one in printf where you need to escape a % with a % if you wish to see it. This is because the % is usually the beginning of a format specifier.