C Programs Tutorials | IT Developer
IT Developer

C Programming - C Escape Sequences



Share with a Friend

C Programming - C Escape Sequences

C Escape Sequences

Escape sequences in C are used to represent special characters that cannot be typed directly in a string or character literal. They start with a backslash (\) followed by a specific character to represent non-printable or control characters.

Commonly Used Escape Sequences

Escape Sequence Description

\n

Newline (Moves cursor to the next line)

\t

Horizontal tab (Inserts a tab space)

\b

Backspace (Moves cursor one step back)

\r

Carriage return (Moves cursor to the beginning of the line)

\\

Backslash (\)

\'

Single quote (')

\"

Double quote (")

\0

Null character (End of a string)

Advanced Escape Sequences

Escape Sequence Description

\f

Form feed (Moves to the next logical page)

\v

Vertical tab

\a

Alert or bell (Produces a beep sound if the system supports it)

\?

Question mark (?)

\ddd

Octal representation of a character (e.g., \101 for 'A')

\xhh

Hexadecimal representation of a character (e.g., \x41 for 'A')

Usage in Strings and Characters

Escape sequences can be used in:

  1. Character Literals: Representing a single character.
  2. String Literals: Representing special characters within a string.

Examples:

C

#include <stdio.h>

int main() {

    printf("Hello\nWorld");   // Outputs "Hello" and moves to the next line for "World"

    printf("\tIndented Text"); // Outputs an indented line

    printf("This is a backslash: \\"); // Outputs a backslash

    printf("\nAlert sound\a"); // Produces a beep sound (if supported)

    return 0;

}

Output:

Hello

World

        Indented Text

This is a backslash: \

Alert sound

Escape Sequences for Non-Printable Characters

Escape Sequence Character Represented

\n

Line feed (LF)

\t

Horizontal tab (HT)

\r

Carriage return (CR)

\f

Form feed (FF)

\v

Vertical tab (VT)

Examples of Octal and Hexadecimal Escape Sequences

  • Octal Representation:

C

char ch = '\101'; // Represents 'A' (ASCII value 65 in octal)

printf("%c", ch); // Outputs: A

  • Hexadecimal Representation:

C

char ch = '\x41'; // Represents 'A' (ASCII value 65 in hexadecimal)

printf("%c", ch); // Outputs: A

Key Points to Remember

  1. Escape sequences are treated as a single character.
    • For example, \n is considered one character, not two.
  2. They are mostly used in string and character literals.
  3. The null character \0 is used to terminate strings in C.
  4. Hexadecimal and octal escape sequences allow representing ASCII values directly.

Practical Example

C

#include <stdio.h>

int main() {

    printf("Special Characters in C:\n");

    printf("Newline: \\n\n");

    printf("Tab: \\t\tThis is indented.\n");

    printf("Backslash: \\\\ \n");

    printf("Double Quote: \\\" \n");

    printf("Alert (Beep): \\a\a\n"); // May produce a beep sound

    return 0;

}

Output:

Special Characters in C:

Newline: \n

Tab: \t This is indented.

Backslash: \

Double Quote: \"

Alert (Beep): \a

Escape sequences make it possible to work with special characters in a human-readable and programmatically efficient way.