Escape sequences in C

CServer Side ProgrammingProgramming

Many programming languages support a concept called Escape Sequence. When a character is preceded by a backslash (\), it is called an escape sequence and it has a special meaning to the compiler. For example, \n in the following statement is a valid character and it is called a new line character −

char ch = '\n';

Here, character n has been preceded by a backslash (\), it has special meaning which is a new line but keep in mind that backslash (\) has special meaning with a few characters only. The following statement will not convey any meaning in C programming and it will be assumed as an invalid statement −

char ch = '\1';

The following table lists the escape sequences available in C programming language −

Sr.NoEscape Sequence & Description
1\t
Inserts a tab in the text at this point.
2\b
Inserts a backspace in the text at this point.
3\n
Inserts a newline in the text at this point.
4\r
Inserts a carriage return in the text at this point.
5\f
Inserts a form feed in the text at this point.
6\’
Inserts a single quote character in the text at this point.
7\”
Inserts a double quote character in the text at this point.
8\\
Inserts a backslash character in the text at this point.

Example

#include <stdio.h>
int main() {
   char ch1;
   char ch2;
   char ch3;
   char ch4;
   ch1 = '\t';
   ch2 = '\n';
   printf( "Test for tabspace %c and a newline %c will start here", ch1, ch2);
}

Output

Test for tabspace and a newline
will start here
raja
Published on 26-Apr-2019 12:13:08
Advertisements