Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22116

Why is "\?" an escape sequence in C/C++?

$
0
0

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:

  1. Why is \? one of the escape sequence characters?
  2. 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.


Viewing all articles
Browse latest Browse all 22116

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>