Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What does backslash really escape? It's used as escaping character. But I always wonder what's escaping?

I know "\n" is a new line character. But what in it is escaping? Why is it called like that?

share|improve this question
It is escaping my wrath when it screws up my strings. – Joel Etherton Oct 6 '11 at 11:34
up vote 23 down vote accepted

The backslash is used as a marker character to tell the compiler/interpreter that the next character has some special meaning. What that next character means is up to the implementation. For example C-style languages use \n to mean newline and \t to mean tab.

The use of the word "escape" really means to temporarily escape out of parsing the text and into a another mode where the subsequent character is treated differently.

share|improve this answer
Depending on the character, a backslash may be telling the compiler/interpreter that the following character has no special meaning. For instance, when escaping quotation marks. – Ryan Kinal Oct 6 '11 at 13:04
Or escaping itself. – Malfist Oct 6 '11 at 14:44


It is a term related to that of an escape sequence and an Esc (escape) key.

The character itself isn't "running away" from any other character. One might say the character following the escape character (say, \n from your example) escapes the fate of being used as an ordinary n in some application, and is used instead as a newline.

share|improve this answer

It's an escape from the parsing context, that is demanding (what a villain) n to be interpreted as n instead of as a newline.

The most interesting distinction about it is that an escape doesn't switch parsing context the way a quote character " (or many other symbols) would do , requiring another " (or some other symbol) to get back to the former context. They just take a little diversion but get back on the road ASAP.

Yeah no one talks anymore about parsing contexts, as much as no one seems to write parsers anymore, since the xml advent. Some problems stop being mainstream, go into a niche and weird niche concepts like that remain lingering around.

share|improve this answer

Escape sequences have a lot in common with the concept of out-of-band communication:

Also sometimes used to describe what communications people call shift characters, such as the ESC that leads control sequences for many terminals, or the level shift indicators in the old 5-bit Baudot codes.

Let's say you're doing a file transfer over a serial port and you negotiate with the computer on the other end: "OK, here comes the file". Since in the general case all characters might be valid file characters (if it's an ASCII file) you have some problems:

  • How do you know where the end of the stream is?
  • Some schemes use ASCII NUL (zero) to terminate, like strings in some languages, but then you can't have that character in the file. That's not allowable in some settings, like file transfers. Zero could be a valid character.
  • You could send the expected file length first, but what if you need to abort?

Various people throughout the history of computing have implemented lots of different methods to get around this problem.

In the case of literal strings in a programming language, it's a similar problem. The first quote says "here comes the string - start reading until you get another quote". Of course, then you have the problem of sending a quote character, so they invented the slash escape sequence. If you see a slash and then a quote, then that's a literal quote, not the end of the string. Unfortunately you just swallowed a spider to eat a fly, because now you can't send the literal slash-quote combination. So, you can escape a slash with another slash. So slash-slash means slash, slash-quote means quote. That allowed them to use slash for other unprintable or hard-to-print characters like carriage returns, line feeds, tabs, etc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.