Escape Sequences

Escape Sequences

Escape sequences are some non-printable character that shows the effect while printing the output. The usage and example of escape sequences are given below.

These are the combination of two characters backslash(\) and one more character but treated as a single character occupy only one byte as other characters.

Escape sequence character Meaning Usage
\a bell alert used to make a sound.
\b backspace used to move the cursor to the one character back.
\n new line used to change the line
\r carriage return used to move the cursor to the beginning of the current line.
\" double quote used to print the double quotes
\' single-quote use to print the double quotes
\f form feed used to change the page.
\t tab used to print the tab

Program to demonstrate the use escape sequence

#include<stdio.h>
int main() {
 	printf("Hello\a\a");
 	printf("Hello\nworld\n");
 	printf("Hello\tWorld\n");
 	printf("Hello \"Alex\"\n");
 	printf("Hello \'Clark\'\n");
 	printf("Hello\bboy\n");
 	printf("Hello\rJk\n");
 	
 	return 0;	
} 

Output

HelloHello
world
Hello   World
Hello "Alex"
Hello 'Clark'
Hellboy
Jkllo

More Controlled on printing

We use the printf() function for printing till now. But print() function has many variations as given below.

Syntax for printing the integer

printf("%wd",variable);
Syntax Explained:

In the above syntax, w stands for white spaces. For print, the given integer variables value, w spaces are reserved and the number is print by right-aligned on the given spaces. If the w is less the number, then the specified width is ignored. Read the following example to understand the fact.

int a=512;
printf("%d",a);//512
printf("%5d",a);//__512
printf("%2d",a);//512
printf("%-5d",a);//512__

The syntax for printing floating-point numbers

printf("%w.pf",variable);
Syntax Explained

In the above example, w stands for the total width reserved for the printing including decimal places and p specifies how many digits(precision) are allowed after the decimal point. Read the following example to understand the fact. By default, 6 digit precision is allowed for float printing.

float f=5.6789;
printf("%f",f);//5.678900
printf("%10f",f);//__5.678900, two blank spaces
printf("%-10f",f);//5.678900__
printf("%10.2f",f);//5.68, round off the decimal places if truncation is performed.
printf("3.2f",f);//5.68

Number format Conversion

If you want to print the decimal number into octal, hexadecimal then this task can be done directly by using the format specifier explained in the following example.

printf("%d",25);//25
printf("%o",25);//31, octal equivalent of decimal 25.
printf("%x",25);//19, hexadecimal equivalent of decimal 25.
If an integer constant is prefixed with zero(0) then constant is called octal constant or if prefixed with (zero-x)0x then constant is called hexadecimal constant.
printf("%d",031);//print 25,031 is an octal integer and 25 is decimal equivalent of oct-31
printf("%d",0x19);//print 25, 0x19 is hexadecimal constant and 25 is decimal equivalent of hex-19.
Note: %i can be also used for the decimal integer printing.