Escape sequence in c

Escape sequences are special characters in C that are used to represent certain non-printable or special characters in a string or character constant. An escape sequence is a combination of a backslash () followed by a character or sequence of characters.

The following are some common escape sequences in C:

  1. \n – Newline: This escape sequence is used to move the cursor to the beginning of the next line.
  2. \t – Horizontal Tab: This escape sequence is used to insert a horizontal tab.
  3. \r – Carriage Return: This escape sequence is used to move the cursor to the beginning of the current line.
  4. \b – Backspace: This escape sequence is used to move the cursor back one space.
  5. \f – Form Feed: This escape sequence is used to insert a form feed character.
  6. ‘ – Single Quote: This escape sequence is used to insert a single quote character.
  7. ” – Double Quote: This escape sequence is used to insert a double quote character.
  8. \0 – Null: This escape sequence is used to insert a null character.

C Escape Sequence Table:-

Some escape sequences are given below.

Escape SequenceMeaning
\nSends the cursor to the next line.
\tHorizontal Tab
\aAlert( Used to generate beep)
\cCarriage return(sends the cursor to the beginning of the current line)
\bbackspace(sends the cursor one character to the left)
\’prints the single quotation mark
\’’prints the double quotation mark
\\prints the backslash character
\?prints the question mark

For example, if you want to print the string “Hello World” with a newline character at the end, you can use the escape sequence \n as follows:

printf("Hello World\n");

Or, if you want to print the string “She said, “Hello!”” with double quotes, you can use the escape sequence ” as follows:

printf("She said, \"Hello!\"\n");

In conclusion, escape sequences are a useful feature in C that allow programmers to insert non-printable or special characters into strings and character constants. By using escape sequences, C programs can handle a wider range of characters and produce more readable and well-formatted output.

Recommended Post: