Escape Sequence Characters in C language

Milan Kathiriya
3 min readNov 29, 2023

--

Escape sequences are special sequences of characters used in strings to represent characters that are difficult or impossible to express directly.

In C language, escape sequences are preceded by a backslash \. Here are some commonly used escape sequences:

1. \n: Newline

Syntax of \n
  • Example: printf("This text\nis on two lines.\n");
Example of \n

2. \t: Tab

Syntax of \t
  • Example: printf("First\tSecond\tThird\n");

One \t means total 8 spaces, counting from first letter (whether it is empty or not)

Example of \t

3. \\: Backslash

  • Example: printf("This is a backslash: \\n");

One backslash \ is used to skip the meaning of next upcoming escape sequence character

Example of \\

4. \b: Backspace

  • Example: printf("Backspace\bThis will erase a character.\n");
Example of \b

5. \r: Carriage return

  • Example: printf("Carriage\rReturn\n");

\r moves the cursor or print head back to the beginning of the line without advancing to the next line.

This means that subsequent characters will overwrite the characters already printed on the current line.

Example of \r in 16 bit compiler (Turbo C++)
Example of \r in 32/64 bit compiler (VS Code)

6. \f: Form feed

  • Example: printf("This is a form feed.\f");

\f is used to advance the printing position to the next page or form. It's often associated with printers where it would cause the printing mechanism to move to the top of the next page.

Example of \f in 32/64 bit compiler (VS Code)

7. \v: Vertical tab

  • Example: printf("Vertical\vTab\n");

\v typically moves the cursor or print head down to the next vertical tab stop. Vertical tab stops are positions on the page where the cursor is supposed to jump vertically.

Example of \v in 32/64 bit compiler (VS Code)

8. \0: Null character

  • Example: char nullChar = '\0';

\0is typically used to assign NULL value to a character. In a string, \0 is used as an ending character or termination character.

Example of \0

Note: \f and \v are not supported in 16 bit compiler (Turbo C++)

These escape sequences are essential for constructing strings with special characters and controlling the formatting of output in C programs.

Thanks for reading this article ❤️

If I got something wrong 🙈, Let me know in the comments. I would like to improve.

Clap 👏 If this article helps you. Add to your 📑 Reading list for future reference.

Connect with me on Linkedin and Github

--

--