Please read the new rule regarding the restriction on the use of AI tools. ×

shuneo's blog

By shuneo, history, 20 months ago, In English
char c = '"';
// it is compiled and successed.

I have a question that: why we can char c = '"'; instead of using char c = '\"'.

I used to think that char c = '"' is wrong before.

Thank you alot

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
20 months ago, # |
  Vote: I like it +9 Vote: I do not like it

Because standard says so. And it always was that way.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf (Section 5.3.13)

»
20 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

We don't need to escape the character " when we put it between '. The reason is just the compiler will understand it and there is no need for \.

But when we put it between " we need to escape it because if we don't it will be marked as the end of the string.

// syntax error
"abcabcabc"defdefdef"
^         ^
start    end