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
- Backspace
- Formfeed
- Newline
- Carriage return
- Horizontal tab
- Null value
- Double quote
- 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.