C++ Escape Character

 In this post I will discussing something called Escape Sequence in C++. 
 
What is Escape Sequence? Escape sequences allows to make the compiler do something such as getting to the next line, performing a TAB action, producing a quick sound (beep). 
 
When we write C++ program, usually we use escape sequence in the program and the compiler will process the output depends on what escape sequence used.The are many escape sequences and they acts differently, and they are always written with a backslash "\" followed by a particular letter or character. 
 
Computers and compilers that follow the American Standard Code Information Interchange (ASCII) [pronounce "as ki"] recognize these escape sequences that have a corresponding ASCII value.

Here are some of the escape sequences.
 

 
These are some explanation for the above escape sequences.
\b – Backspace (BS)
It is ment to move the cursor one character backwards on printers and teletypes to make accents on characters possible. Backspace now often used to not only reposition the cursor, but also delete the actual contents on that position.
\t – Horizontal Tab (HT)
The \t control character in the ASCII character set is defined for layout purposes. It instructs the output device to proceed to the next table column. Table column width is flexible, but on many devices the distance between table columns defaults to 8. Table column is also known as what we call nowadays as TAB, means the distance of space between words. In the earlier time, the use of the TAB not only reduced the work for data typists, but also introduced a method to reduce the amount of storage space necessary for formatted texts. We will now laugh about it, but keep in mind that the ASCII standard was developed 40 years ago when every byte of storage was valuable, and compression methods like ZIP, didn't exist .
\n Line feed (LF)
The line feed character is one of the characters in the ASCII character set that has been misused. At first, the \n or line feed character was ment to move the head of a printer one line down.After that, the second control character carriage return or \r then used to move the printing head to the left margin. This is the way it was implemented in many serial protocols and in operating systems like MS-DOS and Windows. But, C programming language and Unix operating system redefined this character as newline which ment a combination of line feed \n and carriage return \r. Now programmer mainly use the LF character mainly as newline function and most software that handles plain ASCII text files is capable of handling both single LF and CR/LF combinations.
\f Form feed (FF)
The form feed or \f was designed to control the behaviour of printers. When receiving this code the printer moves to the next sheet of paper. The behaviour of the form feed control code depends on the implementation. Some clear the screen, whereas others only display the ^L characters or perform a line feed instead. The shell environments Bash and Tcsh have implemented the ASCII form feed as a clear screen command.
\r – Carriage return (CR)
The carriage return in the ASCII character set in its original form is ment to move the printing head back to the left margin without moving to the next line. In the C programming language and the unix operating system, a redefinition of the LF control code has taken place to newline. Often software now translates an entered \r to the \n ASCII code when the data is stored.

\\ - Backslash
Generally \\ means prints the backslash sign "\" while performing output. Usually in C or C++ programming it is used anywhere to print a backslash output. For example in the code line of                      printf("The backslash sign is \\ .");     //writtten in C language                      cout << "The backslash sign is \\ .";   //writtten in C++ language will be performing this output.                      The backslash sign is \ .
\' - Single quote
It is the same function as Backslash sign. The only difference it is used to prints " ' " single quote sign. For example :                      printf("The single quote sign is \' .");     //writtten in C language                      cout << "The single quote sign is \' .";   //writtten in C++ language will be performing this output.                      The single quote sign is '.
\" - Double quote
Prints double quote " " " sign in the output for the programming code. For example:                      printf("The double quote sign is \" .");     //writtten in C language                      cout << "The double quote sign is \" .";   //writtten in C++ language will be performing this output.                      The double quote sign is ".
So that's all for the escape sequences. You may also check the links below for more reference about ASCII and escape sequences.

MSDN Microsoft
WilsonMar
Lammertbies
ASCII Tables

Writers Note : This articles is edited and adapted to provide simplicity in the topic and understanding about escape sequence. The reference made are from those pages. Finding these information useful? Then share it. Good Day.

No comments :

Post a Comment