{"id":1912,"date":"2024-05-10T06:53:50","date_gmt":"2024-05-10T06:53:50","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=1912"},"modified":"2024-05-10T06:53:50","modified_gmt":"2024-05-10T06:53:50","slug":"escape-sequence-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/escape-sequence-in-c\/","title":{"rendered":"Escape Sequence In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#escape-sequence-in-c\">Escape Sequence In C<\/a><\/li><li ><a href=\"#escape-sequence-list\">Escape Sequence List <\/a><\/li><li ><a href=\"#escape-sequence-in-c-examples\">Escape Sequence in C Examples<\/a><ul><li ><a href=\"#1-example-to-demonstrate-how-to-use-an-escape-sequence-in-c\">1. Example to Demonstrate how to use \\ an Escape Sequence in C<\/a><\/li><li ><a href=\"#2-example-to-demonstrate-how-to-use-b-escape-sequence-in-c\">2. Example to Demonstrate how to use \\b Escape Sequence in C<\/a><\/li><li ><a href=\"#3-example-to-demonstrate-how-to-use-n-escape-sequence-in-c\">3. Example to Demonstrate how to use \\n Escape Sequence in C<\/a><\/li><li ><a href=\"#4-example-to-demonstrate-how-to-use-t-escape-sequence-in-c\">4. Example to Demonstrate how to use \\t Escape Sequence in C<\/a><\/li><li ><a href=\"#5-example-to-demonstrate-how-to-use-v-escape-sequence-in-c\">5. Example to Demonstrate how to use \\v Escape Sequence in C<\/a><\/li><li ><a href=\"#6-example-to-demonstrate-how-to-use-r-escape-sequence-in-c\">6. Example to Demonstrate how to use \\r Escape Sequence in C<\/a><\/li><li ><a href=\"#7-example-to-demonstrate-how-to-use-escape-sequence-in-c\">7. Example to Demonstrate how to use \\\\ Escape Sequence in C<\/a><\/li><li ><a href=\"#8-example-to-demonstrate-how-to-use-and-escape-sequence-in-c\">8. Example to Demonstrate how to use \\\u2019 and \\\u201d Escape Sequence in C<\/a><\/li><li ><a href=\"#9-example-to-demonstrate-how-to-use-escape-sequence-in-c\">9. Example to Demonstrate how to use \\? Escape Sequence in C<\/a><\/li><li ><a href=\"#10-example-to-demonstrate-how-to-use-ooo-escape-sequence-in-c\">10. Example to demonstrate how to use \\ooo escape sequence in C<\/a><\/li><li ><a href=\"#11-example-to-demonstrate-how-to-use-xhh-escape-sequence-in-c\">11. Example to Demonstrate how to use \\xhh Escape Sequence in C<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-escape-sequence-in-c\">FAQ- Escape Sequence In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"escape-sequence-in-c\">Escape Sequence In C<\/h2>\n\n\n\n<p>Escape sequences in C are like secret codes that tell the computer to do special things when we&#8217;re printing text. They help us make our text look and behave the way we want it to. Whether it&#8217;s making a new line, adding spaces, or even doing things we can&#8217;t see, escape sequences are handy tools for making our computer programs look just right. In this guide, we&#8217;ll explore how to use these secret codes in C to make our text and output better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"escape-sequence-list\">Escape Sequence List <\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Escape Sequence<\/th><th>Name<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>\\a<\/td><td>Alarm or Beep<\/td><td>It is used to generate a bell sound in the C program.<\/td><\/tr><tr><td>\\b<\/td><td>Backspace<\/td><td>It is used to move the cursor one place backward.<\/td><\/tr><tr><td>\\f<\/td><td>Form Feed<\/td><td>It is used to move the cursor to the start of the next logical page.<\/td><\/tr><tr><td>\\n<\/td><td>New Line<\/td><td>It moves the cursor to the start of the next line.<\/td><\/tr><tr><td>\\r<\/td><td>Carriage Return<\/td><td>It moves the cursor to the start of the current line.<\/td><\/tr><tr><td>\\t<\/td><td>Horizontal Tab<\/td><td>It inserts some whitespace to the left of the cursor and moves the cursor accordingly.<\/td><\/tr><tr><td>\\v<\/td><td>Vertical Tab<\/td><td>It is used to insert vertical space.<\/td><\/tr><tr><td>\\\\<\/td><td>Backlash<\/td><td>Use to insert backslash character.<\/td><\/tr><tr><td>\\\u2019<\/td><td>Single Quote<br><\/td><td>It is used to display a single quotation mark.<br><br><\/td><\/tr><tr><td>\\\u201d<br><\/td><td>Double Quote<br><\/td><td>It is used to display double quotation marks.<br><br><\/td><\/tr><tr><td>\\?<br><\/td><td>Question Mark<br><\/td><td>It is used to display a question mark.<br><br><\/td><\/tr><tr><td>\\ooo<br><\/td><td>Octal Number<br><\/td><td>It is used to represent an octal number.<br><br><\/td><\/tr><tr><td>\\xhh<br><\/td><td>Hexadecimal Number <br><\/td><td>It represents the hexadecimal number.<br><br><\/td><\/tr><tr><td>\\0<br><\/td><td>NULL<\/td><td>It represents the NULL character.<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"escape-sequence-in-c-examples\">Escape Sequence in C Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-to-demonstrate-how-to-use-an-escape-sequence-in-c\">1. Example to Demonstrate how to use \\ an Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate  \\a escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ output may depend upon the compiler\n    printf(\"My mobile number \"\n           \"is 7\\a8\\a7\\a3\\a9\\a2\\a3\\a4\\a0\\a8\\a\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My mobile number is 7873923408\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-to-demonstrate-how-to-use-b-escape-sequence-in-c\">2. Example to Demonstrate how to use \\b Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate  \\b escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ \\b - backspace character transfers\n    \/\/ the cursor one character back with\n    \/\/ or without deleting on different\n    \/\/ compilers.\n    printf(\"Hello \\b\\b\\b\\b\\b\\bHi Geeks\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi Geeks\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-example-to-demonstrate-how-to-use-n-escape-sequence-in-c\">3. Example to Demonstrate how to use \\n Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to illustrate \\n escape sequence\n#include &lt;stdio.h&gt;\nint main(void)\n{\n    \/\/ Here we are using \\n, which is a new line character.\n    printf(\"Hello\\n\");\n    printf(\"GeeksforGeeks\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello\nGeeksforGeeks<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-example-to-demonstrate-how-to-use-t-escape-sequence-in-c\">4. Example to Demonstrate how to use \\t Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\t escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ Here we are using \\t, which is\n    \/\/ a horizontal tab character.\n    \/\/ It will provide a tab space\n    \/\/ between two words.\n    printf(\"Hello \\t GFG\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello      GFG\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-example-to-demonstrate-how-to-use-v-escape-sequence-in-c\">5. Example to Demonstrate how to use \\v Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\v escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ Here we are using \\v, which\n    \/\/ is vertical tab character.\n    printf(\"Hello friends\\v\");\n \n    printf(\"Welcome to GFG\");\n \n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello friends\n                       Welcome to GFG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-example-to-demonstrate-how-to-use-r-escape-sequence-in-c\">6. Example to Demonstrate how to use \\r Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\r escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ Here we are using \\r, which\n    \/\/ is carriage return character.\n    printf(\"Hello   Geeks \\rGeeksfor\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GeeksforGeeks<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-example-to-demonstrate-how-to-use-escape-sequence-in-c\">7. Example to Demonstrate how to use \\\\ Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\\\(Backslash)\n\/\/ escape sequence to print backslash.\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ Here we are using \\,\n    \/\/ It contains two escape sequence\n    \/\/ means \\ and \\n.\n    printf(\"Hello\\\\GFG\");\n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello\\GFG\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-example-to-demonstrate-how-to-use-and-escape-sequence-in-c\">8. Example to Demonstrate how to use \\\u2019 and \\\u201d Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\' escape\n\/\/ sequence\/ and \\\" escape sequence to\n\/\/ print single quote and double quote.\n#include &lt;stdio.h&gt;\nint main(void)\n{\n    printf(\"\\' Hello Geeks\\n\");\n    printf(\"\\\" Hello Geeks\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>' Hello Geeks\n\" Hello Geeks<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-example-to-demonstrate-how-to-use-escape-sequence-in-c\">9. Example to Demonstrate how to use \\? Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate\n\/\/ \\? escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ Here we are using \\?, which is\n    \/\/ used for the presentation of trigraph\n    \/\/ in the early of C programming. But\n    \/\/ now we don't have any use of it.\n    printf(\"\\?\\?!\\n\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>??!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-example-to-demonstrate-how-to-use-ooo-escape-sequence-in-c\">10. Example to demonstrate how to use \\ooo escape sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\OOO escape sequence\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    \/\/ we are using \\OOO escape sequence, here\n    \/\/ each O in \"OOO\" is one to three octal\n    \/\/ digits(0....7).\n    char* s = \"A\\072\\065\";\n    printf(\"%s\", s);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A:5\n<\/code><\/pre>\n\n\n\n<p>Expalnation <\/p>\n\n\n\n<p>when we see &#8220;\\000&#8221; in the text, it means we&#8217;re dealing with one to three octal digits (0 to 7). This must start with at least one octal digit and can have up to three.<\/p>\n\n\n\n<p>For example, if we have &#8220;\\072&#8221; in the text, this octal notation gets converted to decimal, which is the ASCII value for the character &#8220;:&#8221;. So, wherever we see &#8220;\\072&#8221;, it&#8217;s replaced with &#8220;:&#8221; in the output. For instance, the output might look like &#8220;A:5&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-example-to-demonstrate-how-to-use-xhh-escape-sequence-in-c\">11. Example to Demonstrate how to use \\xhh Escape Sequence in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \\XHH escape\n\/\/ sequence\n#include &lt;stdio.h&gt;\nint main(void)\n{\n    \/\/ We are using \\xhh escape sequence.\n    \/\/ Here hh is one or more hexadecimal\n    \/\/ digits(0....9, a...f, A...F).\n    char* s = \"B\\x4a\";\n    printf(\"%s\", s);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>BJ\n<\/code><\/pre>\n\n\n\n<p>Explanation<\/p>\n\n\n\n<p>when you see &#8220;\\x&#8221; followed by one or more hexadecimal digits (0 to 9, a to f, A to F), it represents a hexadecimal number. It&#8217;s essential to have at least one hexadecimal digit after &#8220;\\x,&#8221; but there can be more than one.<\/p>\n\n\n\n<p>For instance, when you encounter &#8220;\\x4a,&#8221; it&#8217;s a hexadecimal number that gets converted into its decimal equivalent, which happens to be the ASCII value for the character &#8216;J&#8217;. So, wherever you find &#8220;\\x4a,&#8221; you can replace it with &#8216;J&#8217;. Therefore, the output would be &#8220;BJ.&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-escape-sequence-in-c\">FAQ- Escape Sequence In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1694762970908\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the escape sequence?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Escape sequences are special codes used in programming to tell the computer to perform certain actions or display specific characters. These codes are often used to control how text appears on terminals and printers. For example, they can be used to make the text go to the next line (carriage return) or create spaces (tab movements). Escape sequences are also handy for representing characters that can&#8217;t be printed directly or characters with special meanings, like the double quotation mark (&#8220;).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694763038729\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How many escape sequences are there in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The C language provides approximately 15 different escape sequences that give users the ability to format and control how text is displayed on the screen. These escape sequences are powerful tools for customizing the appearance and layout of text in C programs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694763048206\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the two types of escape sequences?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. C provides various escape sequences for text formatting. Two common ones are:<br \/><code>\\n<\/code> (New line): Makes a new line.<br \/><code>\\t<\/code> (Horizontal tab): Adds a horizontal tab. There are more escape sequences for different formatting needs.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Escape Sequence In C Escape sequences in C are like secret codes that tell the computer to do special things when we&#8217;re printing text. They help us make our text look and behave the way we want it to. Whether it&#8217;s making a new line, adding spaces, or even doing things we can&#8217;t see, escape &#8230; <a title=\"Escape Sequence In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/escape-sequence-in-c\/\" aria-label=\"More on Escape Sequence In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5286,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[324],"class_list":["post-1912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-escape-sequence-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=1912"}],"version-history":[{"count":13,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"predecessor-version":[{"id":10611,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1912\/revisions\/10611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5286"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}