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 |
---|---|
\a | alert (bell) character |
\\ | backslash |
\b | backspace |
\? | question mark |
\f | form feed |
\' | single quote |
\n | new line |
\" | double quote |
\r | carriage return |
\ooo | octal number |
\t | horizontal tab |
\xhh | hexadecimal number |
\v | vertical 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.