write a program to show the uses of all the escape sequences
Answers
Answer:
Escape sequences in C
This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. Please help to improve this article by introducing more precise citations. (September 2013) (Learn how and when to remove this template message)
Escape sequences are used in the programming languages C and C++, and their design was copied in many other languages such as 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.
Motivation
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:
#include <stdio.h>
int main() {
printf("Hello,
world!");
}
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),
#include <stdio.h>
int main() {
printf("Hello,%cworld!", 0x0A);
}
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 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:
#include <stdio.h>
int main() {
printf("Hello,\nworld!");
}
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.
This raises the issue of how to represent an actual backslash inside a literal. This is done by using the escape sequence \\, as seen in the next section.
Some languages don't have escape sequences, for example Pascal. Instead a command including a newline would be used (writeln includes a newline, write excludes it).
writeln('Hello');
write('world!');
Table of escape sequences
The following escape sequences are defined in standard C. This table also shows the values they map to in ASCII. However, these escape sequences can be used on any system with a C compiler, and may map to different values if the system does not use a character encoding based on ASCII.
Answer: Most popular Escape Sequences
\\ \
\" "
\' '
\? ?
\a Alert
\b Back space
\n New Line
\t Horizontal tab
\v Vertical tab
\r Carriage return