Escape sequences in C programming



Definition of Escape Sequence

The characters \n were not printed on the screen. The backslash (\) is called an escape character.It indicates that printf is supposed to do something out of the ordinary.

When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence.

The following table list the escape sequences:

Escape sequence Description
\n Newline. Position the cursor at the beginning of the next line.
\t Horizontal tab. Move the cursor to the next tab stop.
\\ Backslash. Insert a backslash character in a string.
\a Alert. Produces a sound or visible alert without changing the current cursor position.
\" Double quote. Insert a double-quote character in a string.

Using Multiple printfs

The printf function can print messages in several different ways. The program below printing on one line with two printf statements.

 
    #include <stdio.h>
        // function main begins program execution
    int main( void ) {
    printf( "Welcome" );
    printf( "to C!\n" );
    }  // end function main                          
     
 
    Output :
    Welcome to C!                         
     

One printf can print several lines by using additional newline characters. Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line.

 
    #include <stdio.h>
    // function main begins program execution
    int main( void ) {
    printf( "Welcome to C!\n" );
    }  // end function main                          
     
 
    Output :
    Welcome 
    to 
    C!                         
     

Ads Right