*** Welcome to piglix ***

Escape sequences in C


Escape sequences are used in the programming languages C and C++, and also in many more languages (with some variations) like Java and C#. An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character. The remainder of this article focuses on C; other programming languages are likely to have different syntax and semantics.

Suppose we want to print out Hello, on one line, followed by world! on the next line. One could attempt to represent the string to be printed as a single literal as follows:

This is not valid in C, since a string literal may not span multiple logical source lines. This can be worked around by printing the newline character using its numerical value (0x0a in ASCII),

This instructs the program to print Hello,, followed by the byte whose numerical value is 0x0a, followed by world!. While this will indeed work when the machine uses the ASCII encoding, it will not work on systems that use other encodings, that have a different numerical value for the newline character. It is also not a good solution because it still does not allow us to represent a newline character inside a literal, and instead takes advantage of the semantics of printf. In order to solve these problems and ensure maximum portability between systems, C interprets \n inside a literal as a newline character, whatever that may be on the target system:

In this code, the escape sequence \n does not stand for a backslash followed by the letter n, because the backslash causes an "escape" from the normal way characters are interpreted by the compiler. After seeing the backslash, the compiler expects another character to complete the escape sequence, and then translates the escape sequence into the bytes it is intended to represent. Thus, "Hello,\nworld!" represents a string with an embedded newline, regardless of whether it is used inside printf or anywhere else.


...
Wikipedia

...