Reputation: 575
In C, why do these two pieces of code give the same output?
#include<stdio.h>
int main(void)
{
const char c='\?';
printf("%c",c);
}
and
#include<stdio.h>
int main(void)
{
const char c='?';
printf("%c",c);
}
I understand that a backslash is used to make quotes ("
or '
) and a backslash obvious to the compiler when we use printf(), but why does this work for the '?'?
Upvotes: 48
Views: 4502
Reputation: 9203
Quoting C11
, chapter §6.4.4.4p4
The double-quote
"
and question-mark?
are representable either by themselves or by the escape sequences\"
and\?
, respectively, but...
.
Emphasis mine
So the escape sequence \?
is treated the same as ?
.
Upvotes: 29
Reputation: 75
when you're defining a char
or string
the compiler parses backslash in that char
or string
as an escape sequence.
Upvotes: 0
Reputation: 81
**
the simple answer of your question is
\? means ?. instead of using \? you can using ? .
\? is escape representation and ? is character representation means both are same.
i have linked a image so that you understand it more easily..
**
"click here to see the image " --> in this image you need to find \? in Escape character
Upvotes: -1
Reputation: 234785
\?
is an escape sequence exactly equivalent to ?
, and is used to escape trigraphs:
#include <stdio.h>
int main(void) {
printf("%s %s", "??=", "?\?="); // output is # ??=
}
Upvotes: 86
Reputation: 409364
Because '\?'
is a valid escape code, and is equal to a question-mark.
Upvotes: 20