There are four special non-alphabet characters that need to be escaped in C/C++: the single quote \'
, the double quote \"
, the backslash \\
, and the question mark \?
. It's apparently because they have special meanings. '
for single char
, "
for string literals, \
for escape sequences, but why is ?
one of them?
I read the table of escape sequences in a textbook today and I realized that I've never escaped ?
before and have never encountered a problem with it. Just to be sure, I tested it under GCC:
#include <stdio.h>
int main(void)
{
printf("question mark ? and escaped \?\n");
return 0;
}
And the C++ version:
#include <iostream>
int main(void)
{
std::cout << "question mark ? and escaped \?" << std::endl;
return 0;
}
Both programs output: question mark ? and escaped ?
So I have two questions:
- Why is
\?
one of the escape sequence characters? - Why does non-escaping
?
work fine? There's not even a warning.
The more interesting fact is that the escaped \?
can be used the same as ?
in some other languages as well. I tested in Lua/Ruby, and it's also true even though I didn't find this documented.