Skip to main content

C Escape sequence

Escape sequences is combination of two or more characters. of c programming have 256 characters. and each have an ASCII code to that meaning are understand by system. escape sequence are start with '\' (backslash) symbol. so backslash and few character are provide a special meaning to system. here given list of Escape sequence and its behaviours.

Iteam ASCII In Decimal Name Overview
\b 8 Backspace if string is contain space then /b are use to remove last character are course move one space backwards.
\n 10 Newline if we are need to newline in output result then use \n.
\r 13 Carriage Return move cursor pointer to beginning of current line.
\f 12 Formfeed Form feed means advance downward, and section separators.
\t 9 Horizontal Tab Append space in 4 character.
\v 11 Vertical Tab print vertical movement of next line string
\ 92 Backslash Print one backslash
\' 39 Single quotation mark Print Single quotation mark
\" 34 Double quotation mark Print Single quotation mark
\? 63 Question mark print Question mark
\a 7 Alert (Beep sound) note that only few compiler are support

Example of Escape sequence

/b (Backspace) : it will move course one space backwards.

/* 
Example Escape sequence ('\b')
*/
/*Header file*/
#include <stdio.h>
int main()
{
 printf("ab\b,cd\b,ef\b \n");
 return 0;
}}

Output

a,c,e 

\n (Newline) :

/* 
Example Escape sequence ('\n')
*/
/*Header file*/
#include <stdio.h>
int main(){
 printf("1\n2\n3\n4\n5");
 return 0;
}

Output

1
2
3
4
5

\r : Example

/* 
  Example Escape sequence ('\r')
  */
  /*Header file*/
#include <stdio.h>
int main(){
  printf("Language \r C programming \n");
  printf("Topic \r Escape sequence \n");
  return 0;
}

Output

 C programming 
 Escape sequence 

\f : Example

/* 
  Example Escape sequence ('\f')
  */
  /*Header file*/
  #include <stdio.h>
  int main(){
  printf("A\fB\fC\fD\fE\fF");
  return 0;
}

Output

A
 B
  C
   D
    E
     F

\t : Example

/* 
  Example Escape sequence ('\t')
  */
  /*Header file*/
#include <stdio.h>
int main(){
  printf("c\tcpp\tjava\tpython");
  return 0;
}

Output

c cpp   java    python

\v : Example

/* 
  Example Escape sequence ('\v')
  */
  /*Header file*/
 #include <stdio.h>
int main() {
  printf("c\vcpp\vjava\vpython");
  return 0;
}

Output

c
 cpp
    java
        python

Print ASCII in Escape sequence

/* 
  print ASCII in Escape sequence
  */
  /*Header file*/
  #include <stdio.h>
  int main() {
  printf("ASCII IN DECIMAL\n");
  printf("\\t : %d\n",'\t');
  printf("\\b : %d\n",'\b');
  printf("\\v : %d\n",'\v');
  printf("\\f : %d\n",'\f');
  printf("\\a : %d\n",'\a');
  printf("\\? : %d\n",'\?');
  printf("\\n : %d\n",'\n');
  printf("\\r : %d\n",'\r');
  printf("\\' : %d\n",'\'');
  printf("\\  : %d\n",'\\');
  printf("\"  : %d\n",'\"');
  return 0;
}

Output:

ASCII IN DECIMAL
\t : 9
\b : 8
\v : 11
\f : 12
\a : 7
\? : 63
\n : 10
\r : 13
\' : 39
\  : 92
"  : 34

Hex escape sequence

hex escape sequence example.

/* 
 Example of hex Escape sequence
*/
/*Header file*/
#include <stdio.h>
int main(){
  //print C O D E
  printf("\x43 \x4f \x44 \x45");
  return 0;
}

Output

C O D E
Hex Value Hex Value Hex Value
\x21 ! \x22 " \x23 #
\x24 $ \x25 % \x26 &
\x27 ' \x28 ( \x29 )
\x30 0 \x31 1 \x32 2
\x33 3 \x34 4 \x35 5
\x36 6 \x37 7 \x38 8
\x39 9 \x40 @ \x41 A
\x42 B \x43 C \x44 D
\x45 E \x46 F \x47 G
\x48 H \x49 I \x50 P
\x51 Q \x52 R \x53 S
\x54 T \x55 U \x56 V
\x57 W \x58 X \x59 Y
\x60 ` \x61 a \x62 b
\x63 c \x64 d \x65 e
\x66 f \x67 g \x68 h
\x69 i \x70 p \x71 q
\x72 r \x73 s \x74 t
\x75 u \x76 v \x77 w
\x78 x \x79 y \x4a J
\x4b K \x4c L \x4d M
\x4e N \x4f O \x6a j
\x6b k \x6c l \x6d m
\x6e n \x6f o \x7A z
\x7B { \x7C | \x7D }
\x7E ~




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment