PDF

Escape Sequences and Format Specifiers

1 comment
Character combinations consisting of a backslash ' \ '  followed by letter or by a combination of digits are called "Escape Sequences". To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant. 

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ( " ).

The following table lists the ANSI escape sequences and what they represent.

List of  Escape Sequences


"\a       Audible Bell (  To ring a beep )

"\b"         Backspace  ( It moves one space back )

"\n"        New line  ( It moves control to next line )

"\f"        From feed  (  It moves one page next ) [ It's used in printing the whole copy only ]

"\t" Horizontal Tab  ( It moves five spaces of 'Tab' ) [ User can also set 'Tab' space 
                according his/her requirement ]

"\v"         Vertical Tab  ( It moves five lines down ) [ It does not work in Turbo-c Compiler ]

"\r"        Carriage Return ( It replaces a word or string from the beginning of the other other
               string ) 
             [ Ex. printf("\n Kite is beautiful \r Bike");  Will display the output:  Bike is beautiful ]

"\'"          It displays a single quote

"\""         It displays double quotes

"\?"         It displays question mark

"\0"         Null character  ( It tells us the end of the string and is used in string handling )


Example:


#include<iostream.h>
int main()
{
printf("This\nis\na\ntest\nShe said,\"How are you?\"\n");
}


Output:


This
is 
a
test

She said,"How are you?"



List of Format Specifiers



%c   The character formate specifier.

%d   The integer format specifier.

%i   The integer format specifier ( same as %d )

%f    The floating-point format specifier.

%o   The unsigned octal format specifier.

%s   The string format specifier.

%u   The unsigned integer format specifier.

%x   The unsigned hexadecimal format specifier.

%X   The unsigned hexadecimal format specifier.


1 comment: