Escape sequences in C programming language explained

Escape sequences in c programming language are basically used for formatting output and it is usually found in ‘printf’ statements. Generally, they begin with ‘\’. In this post, escape sequences in c programming language along with examples will be discussed

Some of the important escape sequences in C programming are as follows

  1. Backspace
  2. Formfeed
  3. Newline
  4. Carriage return
  5. Horizontal tab
  6. Null value
  7. Double quote
  8. Back Slash

Backspace

‘\b’ is used for backspace. It removes a character written immediately before it.

Example:

#include <stdio.h>

void main()
{
printf(“My example for backspacee\b. See carefully the extra e in backspace is removed”);
return;
}

Output:

My example for backspace. See carefully the extra e in backspace is removed

In the output, the extra ‘e’ in backspace is removed because of ‘\b’.

Formfeed

‘\f’ is used for formfeed. It prints output in certain style as shown in the below example.

Example:

#include <stdio.h>

void main()
{
printf(“Hello\fWorld”);
return;
}

Output:

Hello

      World

Instead of ‘Hello World’. It prints in the above formatting.

Newline: One of the most frequently used escape sequences in C programming language

‘\n’ is used for newline in C programming. It prints characters followed by a newline escape sequence in a new line.

Example:

#include <stdio.h>

void main()
{
printf(“Hello\nWorld”);
return;
}

Output:

Hello
World

In the output, it printed World in the new line.

Carriage return

‘\r’ is used for carriage return. It returns the cursor position to the beginning of the line..

Example:

#include <stdio.h>

void main()
{
printf(“Hello World\rPretty”);
return;
}

Output:

PrettyWorld

It first prints Hello World and then return courser to the beginning of the line and then starts replacing the characters with Pretty

Horizontal tab: One of the most frequently used escape sequences in C programming language

‘\t’ is used for horizontal in C programming. It adds ‘horizontal tab’ between two characters.

Example:

#include <stdio.h>

void main()
{
printf(“Hello World\n”);
printf(“Hello\tWorld”);
return;
}

Output:

Hello World
Hello World

The above show the normal space between two words and impact of \t.

Null value

‘\0’ is used for Null value. Anything written after it will not be neglected.

Example:

#include <stdio.h>

void main()
{
printf(“Hello\0World”);
return;
}

Output:

Hello

Double quotes

\” is used to print a double quotation mark on the output screen.

Example:

#include <stdio.h>

void main()
{
//printf(“My “program” to print the double quotation mark”);
printf(“My \”program\” to print the double quotation mark”);
return;
}

Output:

My “program” to print the double quotation mark

In line 4 of the program, if // is removed, it will give error as for printing double quotation, escape sequence is mandatory.

Backslash

In order to print backslash, \, on the output screen, back slash escape sequence, ‘\\’ is used.

Example:

#include <stdio.h>

void main()
{
//printf(“My program to print the \”);
printf(“My program to print the \”);
return;
}

Output:

My program to print the \

In line 4 of the program, if // is removed, it will give an error as for printing backslash double quotation, the escape sequence is mandatory.

Data types, their sizes in C programming