为什么 '?'和'\?'在C中给出相同的输出?

时间:2018-05-17 09:23:23

标签: c escaping

在C中,为什么这两段代码会给出相同的输出?

#include<stdio.h>

int main(void)
{
    const char c='\?';
    printf("%c",c);
}

#include<stdio.h>

int main(void)
{
    const char c='?';
    printf("%c",c);
}

据我所知,当我们使用printf()时,反斜杠用于生成引号("')和编译器的反斜杠,但为什么这对'?'有效?

5 个答案:

答案 0 :(得分:86)

\?是一个完全等同于?的转义序列,用于转义trigraphs

#include <stdio.h>
int main(void) {
    printf("%s %s", "??=", "?\?="); // output is # ??=
}

答案 1 :(得分:28)

引用C11,章§6.4.4.4p4

  

双引号"和问号?可以单独表示 ,也可以表示转义序列\" {{1分别是\?

强调我的

因此转义序列...的处理方式与\?相同。

答案 2 :(得分:20)

因为'\?'是有效的escape code,并且等于问号。

答案 3 :(得分:0)

当您定义conda install numpychar时,编译器将stringchar中的反斜杠解析为转义序列。

答案 4 :(得分:-1)

**

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.

我已将图片链接起来,以便您更轻松地理解它。

**

"click here to see the image " --> in this image you need to find \? in Escape character