Playing with escape sequences in C language -
Well , escape sequences hold a really important part in C programming language and are often used in formatted control strings in library functions like printf() and scanf().
An escape sequence is a sequence of characters which is used inside formatted control strings and it produces an output which is difficult or almost impossible to produce directly. (for example system bell is almost impossible to produce directly and so we use an escape sequence for producing it.)
                                Usage of escape sequences
Letâs try to understand some of the most used escape sequences one by one -
\a (alarm) - These are used for creating system bells or alarm and can be used to alarm user about occurrence of certain event. For example , in the following code when the variable will reach value of 10 in the for-loop then userâs will be indicated by a system bell -
\n (newline) - These are used for moving the cursor to the next line. For example - For the following code the output will be produced as -
\t (Horizontal tab) - Using \t will move the code to a distance horizontally which is equivalent to a tab space to the right. For example - For the following code the output will be produced as -
AÂ Â Â Â Â Â Â AÂ Â Â Â Â Â Â L
\v(Vertical tab) - Using \v will move the position of the cursor in vertical tab space.For example - For the following code the output will be produced as -
            Loknath
                          Panigrahi
printf(âAaashish\vLoknath\vPanigrahiâ);
\b(Backspace) - Using \b will exactly produce the same effect as what would be produces when you will enter the backsapce button in your keyboard and so it \b actually helps in removing previous character of a string.For example - For the following code the output will be produced as -
printf(âA\ba\bs\bh\bi\bs\bh\nâ);
printf(âh\bs\bi\bh\bs\ba\bAâ);
\? (Question mark) - Using \? will help in producing question mark in the formatted control string as shown below -
printf(âWhat is your name\?\t:â);
The above code will produce output as - What is your name?     :
\â and \â (single and double quotation marks) - \â when used in a formatted control string will produce a single quotation mark and \â will produce a double quotation mark. The reason why we need \â is that in argument we already have are using ââ and using another â will produce an error and so we need this.
An example representing this has been shown below -
printf(â\âHello world\â\nâ);
printf(â\âHello world\â\nâ);
The above code will produce an output -
\r (carriage return) - Carriage return allows to cursor of the program back to the beginning of the line but it also removes the text before. You can understand this by the following example -
printf(âA\ra\rs\rh\ri\rs\rh\nâ);
printf(â A\ra\rs\rh\ri\rs\rhâ);
The above code will produce an output -
Why in the 2nd line is the output hA whereas the output in the 1st line is h ?? Itâs because in the first line every time when the cursor moves to front the previous character is deleted hence output is h where as in the second printf statement , the 1st character is not A but it is space hence while performing carriage return space will be replace by the character ahead and at the end the output will be hA.