Hi, I was looking at the escape sequences for characters in strings in c++ and I noticed there is an escape sequence for a question mark. Can someone tell me why this is? It just seems a little odd and I can't figure out what ? does in a string. Thanks.
+12
A:
It's to keep a question mark from getting misinterpreted as part of a trigraph.
For example, in
"What??!"
The "??! would be interpreted as the |
character. So, you have to escape the question marks as follows:
"What\?\?!"
Example complements of http://msdn.microsoft.com/en-us/library/bt0y4awe%28VS.80%29.aspx
James McNellis
2009-10-19 03:32:56
By default, gcc disables the interpretation of trigraphs, unless they're explicitly enabled with `-trigraphs`, `-ansi`, or some of the `-std=xxx` options. `-Wtrigraphs` (enabled by `-Wall`) also enables warnings for trigraph sequences.
Adam Rosenfield
2009-10-19 03:50:29
All about trigraphs: http://stackoverflow.com/questions/1234582/purpose-of-trigraph-sequences-in-c/1234618#1234618
Michael Burr
2009-10-19 05:36:14
You learn something new every day...
GRB
2009-10-19 06:48:50
You just ignore the existence of trigraphs... until they bite you in the ass.
Matthieu M.
2009-10-19 15:28:45
@Mattihieu - that's pretty much what I do and it's really not a problem. The tradeoff of once every 7 or 8 years relearning why I hate trigraphs is an acceptable cost to not worrying about them at any other time. But, GCC is right to disable them by default (I think).
Michael Burr
2009-10-19 22:29:30