Why Is "\" an Escape Sequence in C/C++

What's the purpose of the \' escape sequence?

You need it if you want a character literal:

char apos = '\'';

warning: unknown escape sequence '\

In C string literals the \ has a special meaning, it's for representing characters such as line endings \n. If you want to put a \ in a string, you need to use \\.

For example

"\\Hello\\Test"

will actually result in "\Hello\Test".

So your regexp needs to be written as:

"[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\\}\\\\.[0-9]\\{1,3\\}"

instead of:

"[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}"

Sure this is painful because \ is used as escape character for the regexp and again as escape character for the string literal.

So basically: when you want to put a \ you need to write \\.

What do the characters starting with '\' and followed by a number e.g '\234' mean?

It's an escape sequence, for octal values. The syntax is \nnn.

You can read more about escape sequences in c here.

Garbage is printed, because 233 in octal is 155 in decimal, 234 is 156 and 235 is 157. They do not represent any ascii character.

How do I escape the `\0 character` or why is my method not working? (C++, GTest)

To embed zero in a string literal, write \0 or \x00. But in most contexts, char arrays (including string literals) are treated as C strings, i.e. zero-terminated. Thus the first embedded zero will be considered as string end and not its part. Indeed, that is the only way as the size is not passed along string (even string literal, although it has one).

So, you need:

  1. Return your string from url_to_qname as something that supports embedded zeroes. std::string is okay.
  2. Pass the string literal in a similar way. In C++17, you can use std::string_view for that: write your literal as "\x06google\x03com\x00"sv (after using namespace std::literals or likewise) and it will be converted to std::string_view in a way preserving embedded zeroes. Before C++17 that would need a bit of template or macro magic though, as things like std::string("\x06google\x03com\x00") don’t work (that constructor supports C strings only).

UPDATE: as @273K pointed out there is also ""s which is available a bit earlier, since C++14.

Rules for C++ string literals escape character

Control characters:

(Hex codes assume an ASCII-compatible character encoding.)

  • \a = \x07 = alert (bell)
  • \b = \x08 = backspace
  • \t = \x09 = horizonal tab
  • \n = \x0A = newline (or line feed)
  • \v = \x0B = vertical tab
  • \f = \x0C = form feed
  • \r = \x0D = carriage return
  • \e = \x1B = escape (non-standard GCC extension)

Punctuation characters:

  • \" = quotation mark (backslash not required for '"')
  • \' = apostrophe (backslash not required for "'")
  • \? = question mark (used to avoid trigraphs)
  • \\ = backslash

Numeric character references:

  • \ + up to 3 octal digits
  • \x + any number of hex digits
  • \u + 4 hex digits (Unicode BMP, new in C++11)
  • \U + 8 hex digits (Unicode astral planes, new in C++11)

\0 = \00 = \000 = octal ecape for null character

If you do want an actual digit character after a \0, then yes, I recommend string concatenation. Note that the whitespace between the parts of the literal is optional, so you can write "\0""0".



Related Topics



Leave a reply



Submit