Unknown meta-character in C/C++ string literal?

2019-06-20 01:01发布

I created a new project with the following code segment:

char* strange = "(Strange??)";
cout << strange << endl;

resulting in the following output:

(Strange]

Thus translating '??)' -> ']'

Debugging it shows that my char* string literal is actually that value and it's not a stream translation. This is obviously not a meta-character sequence I've ever seen. Some sort of Unicode or wide char sequence perhaps? I don't think so however... I've tried disabling all related project settings to no avail.

Anyone have an explanation?

  • search : 'question mark, question mark, close brace' c c++ string literal

9条回答
淡お忘
2楼-- · 2019-06-20 01:55

??) is a trigraph.

查看更多
Animai°情兽
3楼-- · 2019-06-20 01:58
老娘就宠你
4楼-- · 2019-06-20 01:58

As mentioned several times, you're being bitten by a trigraph. See this previous SO question for more information:

You can fix the problem by using the '\?' escape sequence for the '?' character:

char* strange = "(Strange\?\?)";

In fact, this is the reason for that escape sequence, which is somewhat mysterious if you're unaware of those damn trigraphs.

查看更多
登录 后发表回答