2

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?

MSalters
  • 173,980
  • 10
  • 155
  • 350
jaap-de-boer
  • 137
  • 1
  • 1
  • 8

1 Answers1

4

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.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • Never heard of "Tri-graph". Thanks! – jaap-de-boer Feb 19 '16 at 11:33
  • A useful workaround would be to split the Tri-graph after the two questionmarks: `printf("??" "-")`. This definitely works :) – jaap-de-boer Feb 19 '16 at 11:40
  • You can usually turn them off in most compilers. This means your code does not strictly conform to the standard, but it's rare anyone actually wants a di/trigraph these days. – Joe Feb 19 '16 at 11:43
  • They're already removed from the draft for the next Standard (C++17), so it makes sens for modern compilers to drop the feature. VS2008 of course isn't C++17-aware ;) – MSalters Feb 19 '16 at 11:52