Write a program to show the effect of various escape sequence.

#include<conio.h>
#include<stdio.h>
int main()
{
	int a=1,b=a+1,c=b+1,d=c+1;
	printf("\t A=%d \n B=%d \'c=%d\'",a,b,c); 
	printf("\n\b***D=%d**",d);
	printf("\n**********");
	printf("\r A=%d B=%d",a,b);
	getch();
}

/*print the valu of 'a' after a tab due to '\t'
The three prefix '*' are written followed by the '\b'.The '\b' overrite the last character,
In third printf() statement only the sequence of '*' are printed,but in the output only half line is 
displayed because it is affected by the fourth printf() statement.
In fourth Statement '\r' is used which reverse the printable area one line before from the current 
location.*/ 

The program uses several escape sequences to format the output:

  1. \t – this is the tab character, which inserts a horizontal tab (or several spaces) into the output. In this case, it is used to move the “A=” to the right before the value of a is printed.
  2. \' – this is an escaped single quote character. The backslash is used to indicate that the single quote should be printed as a literal character, rather than being interpreted as the end of the string. In this case, it is used to put single quotes around the value of c.
  3. \b – this is the backspace character, which moves the cursor back one character. In this case, it is used to overwrite the last character printed, which is the space after the third asterisk in the third printf() statement.
  4. \r – this is the carriage return character, which moves the cursor to the beginning of the current line. In this case, it is used to print the values of a and b on the same line, overwriting the previous output.

The program declares four variables, a, b, c, and d, and initializes them with sequential values. The printf() statements use various escape sequences to format the output of these variables in different ways.

Thanks

Write a program to show the effect of various escape sequence.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top