Home > Software engineering >  What is the purpose of d-char-sequence in C raw strings?
What is the purpose of d-char-sequence in C raw strings?

Time:03-21

On this reference: https://en.cppreference.com/w/cpp/language/string_literal, raw string is defined as:

 prefix(optional) R"d-char-sequence(optional)(r-char-sequence(optional))d-char-sequence(optional)"

Example:

const char* s1 = R"foo(
Hello
  World
)foo";

What is the purpose of the d-char-sequence ("foo" in the example above)?

CodePudding user response:

The optional d-char-sequence is used to define the end marker of the raw string.

For example, if the raw string contains the substring )", then the line:

const char* s = R"(string with )" inside)";

will raise a syntax error. This can be fixed by using the optional d-char-sequence that is not met inside the raw string:

const char* s = R"uniq(string with )" inside)uniq";

CodePudding user response:

It lets you embed )" in the literal, which would otherwise be considered the end of the literal.

  •  Tags:  
  • c
  • Related