How to use Escape Sequences in printf

Escape Sequences in printf

Escape sequences are the special directives used to format printing.

For example, \n indicates that the next printing should start from the first column of the next line.

printf requires the backslash character - an escape sequence - to display some special characters.

Sequence Meaning
\a Beeps the speaker
\b Backspace (moves the cursor back, no erase)
\f Form feed (ejects printer page; may clear the screen on some computers)
\n Newline, like pressing the Enter key
\r Carriage return (moves the cursor to the beginning of the line)
\t Tab
\v Vertical tab (moves the cursor down a line)
\\ The backslash character
\' The apostrophe
\" The double-quote character
\? The question mark
\0 The "null" byte (that's 0, not the letter O)
\Onn A character value in octal (base 8)
\xnnn A character value in hexadecimal (base 16)

Example 1

Use escape sequence to display new line character.


#include <stdio.h>
main(){
  printf("Hi \n");
}

The code above generates the following result.





















Home »
  C Language »
    Input / Output »




printf
scanf