Joey
Joey

Reputation: 354566

What does a double backslash at the end of a line do?

I'm looking through a bit of C++ code here at work and stumbled across something like this:

char numberlist[5000] = 
  "{42, 42, 42, 42, 42, 42, 42, 42, 42, \
   42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, \
   42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, \\
   // A few more lines, all ending in double-backslash
   42, 42, 42, 42, 42}";

(contents redacted to protect whatever it may mean). Now, I do know what a single backslash at the end of a line swallows the following line break, essentially concatenating the two lines. But a double backslash?

I get a warning here:

warning C4129: ' ' : unrecognized character escape sequence

Syntax highlighting for a string stops at the end of the first double-backslashed line (in VS2010). Could it be that the backslash-newline gets eaten first and then the remaining backslash-space gets interpreted as an escape sequence? And is it safe to just remove the second backslash here?

Upvotes: 3

Views: 3056

Answers (2)

Joey
Joey

Reputation: 354566

Apparently my hunch was correct and this first removes the backslash-linefeed sequence, which leaves backslash-space, which emits the warning and resolves to just a space. I just looked into the debugger to confirm that there are no backslashes in the string itself. So removing the second backslash does not alter the code's meaning (and removes a warning, or rather 15 of them or so).

Upvotes: 0

user4815162342
user4815162342

Reputation: 154996

Since the double backslash is inside a string literal, its meaning is clear: it resolves to a single \ character in the string.

The strings continues with a literal newline, which makes the source not syntactically valid C++, and which is also why syntax highlighting stops at that point. Apparently Visual C++ allows such strings anyway (as does g++), so compilation succeeds.

The warning for unrecognized character escape sequence appears unrelated, since both \<newline> and \\ are themselves valid escapes.

Upvotes: 1

Related Questions