Escape Sequence In C https://www.skillvertex.com/blog Fri, 10 May 2024 06:53:50 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Escape Sequence In C https://www.skillvertex.com/blog 32 32 Escape Sequence In C https://www.skillvertex.com/blog/escape-sequence-in-c/ https://www.skillvertex.com/blog/escape-sequence-in-c/#respond Fri, 10 May 2024 06:53:50 +0000 https://www.skillvertex.com/blog/?p=1912 Read more]]>

Table of Contents

Escape Sequence In C

Escape sequences in C are like secret codes that tell the computer to do special things when we’re printing text. They help us make our text look and behave the way we want it to. Whether it’s making a new line, adding spaces, or even doing things we can’t see, escape sequences are handy tools for making our computer programs look just right. In this guide, we’ll explore how to use these secret codes in C to make our text and output better.

Escape Sequence List

Escape SequenceNameDescription
\aAlarm or BeepIt is used to generate a bell sound in the C program.
\bBackspaceIt is used to move the cursor one place backward.
\fForm FeedIt is used to move the cursor to the start of the next logical page.
\nNew LineIt moves the cursor to the start of the next line.
\rCarriage ReturnIt moves the cursor to the start of the current line.
\tHorizontal TabIt inserts some whitespace to the left of the cursor and moves the cursor accordingly.
\vVertical TabIt is used to insert vertical space.
\\BacklashUse to insert backslash character.
\’Single Quote
It is used to display a single quotation mark.

\”
Double Quote
It is used to display double quotation marks.

\?
Question Mark
It is used to display a question mark.

\ooo
Octal Number
It is used to represent an octal number.

\xhh
Hexadecimal Number
It represents the hexadecimal number.

\0
NULLIt represents the NULL character.

Escape Sequence in C Examples

1. Example to Demonstrate how to use \ an Escape Sequence in C

// C program to illustrate  \a escape sequence
#include <stdio.h>
 
int main(void)
{
    // output may depend upon the compiler
    printf("My mobile number "
           "is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
    return (0);
}

Output

My mobile number is 7873923408

2. Example to Demonstrate how to use \b Escape Sequence in C

// C program to illustrate  \b escape sequence
#include <stdio.h>
 
int main(void)
{
    // \b - backspace character transfers
    // the cursor one character back with
    // or without deleting on different
    // compilers.
    printf("Hello \b\b\b\b\b\bHi Geeks");
    return (0);
}

Output

Hi Geeks

3. Example to Demonstrate how to use \n Escape Sequence in C


// C program to illustrate \n escape sequence
#include <stdio.h>
int main(void)
{
    // Here we are using \n, which is a new line character.
    printf("Hello\n");
    printf("GeeksforGeeks");
    return (0);
}

Output

Hello
GeeksforGeeks

4. Example to Demonstrate how to use \t Escape Sequence in C

// C program to illustrate \t escape sequence
#include <stdio.h>
 
int main(void)
{
    // Here we are using \t, which is
    // a horizontal tab character.
    // It will provide a tab space
    // between two words.
    printf("Hello \t GFG");
    return (0);
}

Output

Hello      GFG

5. Example to Demonstrate how to use \v Escape Sequence in C

// C program to illustrate \v escape sequence
#include <stdio.h>
 
int main(void)
{
    // Here we are using \v, which
    // is vertical tab character.
    printf("Hello friends\v");
 
    printf("Welcome to GFG");
 
    return (0);
}

Output

Hello friends
                       Welcome to GFG

6. Example to Demonstrate how to use \r Escape Sequence in C

// C program to illustrate \r escape sequence
#include <stdio.h>
 
int main(void)
{
    // Here we are using \r, which
    // is carriage return character.
    printf("Hello   Geeks \rGeeksfor");
    return (0);
}

Output

GeeksforGeeks

7. Example to Demonstrate how to use \\ Escape Sequence in C

// C program to illustrate \\(Backslash)
// escape sequence to print backslash.
#include <stdio.h>
 
int main(void)
{
    // Here we are using \,
    // It contains two escape sequence
    // means \ and \n.
    printf("Hello\\GFG");
    return (0);
}

Output

Hello\GFG

8. Example to Demonstrate how to use \’ and \” Escape Sequence in C

// C program to illustrate \' escape
// sequence/ and \" escape sequence to
// print single quote and double quote.
#include <stdio.h>
int main(void)
{
    printf("\' Hello Geeks\n");
    printf("\" Hello Geeks");
    return 0;
}

Output

' Hello Geeks
" Hello Geeks

9. Example to Demonstrate how to use \? Escape Sequence in C

// C program to illustrate
// \? escape sequence
#include <stdio.h>
 
int main(void)
{
    // Here we are using \?, which is
    // used for the presentation of trigraph
    // in the early of C programming. But
    // now we don't have any use of it.
    printf("\?\?!\n");
    return 0;
}

Output

??!

10. Example to demonstrate how to use \ooo escape sequence in C

// C program to illustrate \OOO escape sequence
#include <stdio.h>
 
int main(void)
{
    // we are using \OOO escape sequence, here
    // each O in "OOO" is one to three octal
    // digits(0....7).
    char* s = "A\072\065";
    printf("%s", s);
    return 0;
}

Output

A:5

Expalnation

when we see “\000” in the text, it means we’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.

For example, if we have “\072” in the text, this octal notation gets converted to decimal, which is the ASCII value for the character “:”. So, wherever we see “\072”, it’s replaced with “:” in the output. For instance, the output might look like “A:5”.

11. Example to Demonstrate how to use \xhh Escape Sequence in C

// C program to illustrate \XHH escape
// sequence
#include <stdio.h>
int main(void)
{
    // We are using \xhh escape sequence.
    // Here hh is one or more hexadecimal
    // digits(0....9, a...f, A...F).
    char* s = "B\x4a";
    printf("%s", s);
    return 0;
}
BJ

Explanation

when you see “\x” followed by one or more hexadecimal digits (0 to 9, a to f, A to F), it represents a hexadecimal number. It’s essential to have at least one hexadecimal digit after “\x,” but there can be more than one.

For instance, when you encounter “\x4a,” it’s a hexadecimal number that gets converted into its decimal equivalent, which happens to be the ASCII value for the character ‘J’. So, wherever you find “\x4a,” you can replace it with ‘J’. Therefore, the output would be “BJ.”

FAQ- Escape Sequence In C

Q1. What is the escape sequence?

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’t be printed directly or characters with special meanings, like the double quotation mark (“).

Q2. How many escape sequences are there in C?

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.

Q3. What are the two types of escape sequences?

Ans. C provides various escape sequences for text formatting. Two common ones are:
\n (New line): Makes a new line.
\t (Horizontal tab): Adds a horizontal tab. There are more escape sequences for different formatting needs.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/escape-sequence-in-c/feed/ 0