C String Escape

Description

\ is the escape sequence that sticks a backslash character into a string.

Escape char

Escaped characters are character which we cannot input directly from keyboard. For example, new line character. The following table lists the escaped characters we often use.

Escape sequence Value
\aalert (bell) character
\\backslash
\bbackspace
\?question mark
\fform feed
\'single quote
\nnew line
\"double quote
\rcarriage return
\ooooctal number
\thorizontal tab
\xhhhexadecimal number
\vvertical tab

Example 1

\\ is used to escape a backslash character. The first \ is the escape character and the second \ is the real character we would like to output.

 
#include <stdio.h>
int main()//from   w ww .  j av  a 2s  . c o  m
{
   printf(" \\ string.");
  
   return 0;
} 

The code above generates the following result.

Example 2

We can escape a single quote with \'.


#include<stdio.h>
/*from  w  w  w.ja  v  a2s . c  o  m*/
int main(void)
{
  printf("If at first you don\'t succeed, try, try, try again!");
  return 0;
}

The code above generates the following result.

Example 3

Add new line character


#include <stdio.h>
//from   w w  w  .j a  v  a 2  s . c o m
int main(void)
{
  printf("A\nB\nC");
  return 0;
}

The code above generates the following result.

Example 4

Escape Quotations


#include <stdio.h>
//from  www .  j a v  a2 s.  com
int main(void)
{
  printf("\n\"It is a wise father that knows his own child.\" Shakespeare");
  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing