Wednesday, November 2, 2011

Escape Sequences in C programming language

We saw earlier how the newline character, \n, when inserted in a printf( )’s format string, takes the cursor to the beginning of the next line. The newline character is an ‘escape sequence’, so called because the backslash symbol (\) is considered as an ‘escape’ character—it causes an escape from the normal interpretation of a string, so that the next character is recognized as one having a special meaning.
The following example shows usage of \n and a new escape sequence \t, called ‘tab’. A \t moves the cursor to the next tab stop. A 80-column screen usually has 10 tab stops. In other words, the screen is divided into 10 zones of 8 columns each. Printing a tab takes the cursor to the beginning of next printing zone. For example, if cursor is positioned in column 5, then printing a tab takes it to column 8.
 
main( )
{
printf ( "You\tmust\tbe\tcrazy\nto\thate\tthis\tbook" ) ;
}

And here’s the output...
1 2 3 4
01234567890123456789012345678901234567890

You must be crazy
to hate this book
The \n character causes a new line to begin following ‘crazy’. The tab and newline are probably the most commonly used escape sequences, but there are others as well. Figure 11.4 shows a complete list of these escape sequences.


The first few of these escape sequences are more or less self- explanatory. \b moves the cursor one position to the left of its current position. \r takes the cursor to the beginning of the line in which it is currently placed. \a alerts the user by sounding the speaker inside the computer. Form feed advances the computer stationery attached to the printer to the top of the next page. Characters that are ordinarily used as delimiters... the single quote, double quote, and the backslash can be printed by preceding them with the backslash. Thus, the statement,
printf ( "He said, \"Let's do it!\"" ) ;
will print...
He said, "Let's do it!"
So far we have been describing printf( )’s specification as if we are forced to use only %d for an integer, only %c for a char, only %s for a string and so on. This is not true at all. In fact, printf( ) uses the specification that we mention and attempts to perform the specified conversion, and does its best to produce a proper result. Sometimes the result is nonsensical, as in case when we ask it to print a string using %d. Sometimes the result is useful, as in the case we ask printf( ) to print ASCII value of a character using %d. Sometimes the result is disastrous and the entire program blows up.
The following program shows a few of these conversions, some sensible, some weird.
 
main( )
{
char ch = 'z' ;
int i = 125 ;
float a = 12.55 ;
char s[ ] = "hello there !" ;
printf ( "\n%c %d %f", ch, ch, ch ) ;
printf ( "\n%s %d %f", s, s, s ) ;
printf ( "\n%c %d %f",i ,i, i ) ;
printf ( "\n%f %d\n", a, a ) ;
}

And here’s the output ...
z 122 -9362831782501783000000000000000000000000000.000000
hello there ! 3280 -9362831782501783000000000000000000000000000.000000
} 125 -9362831782501783000000000000000000000000000.000000
12.550000 0
I would leave it to you to analyze the results by yourselves. Some of the conversions you would find are quite sensible.
Let us now turn our attention to scanf( ). scanf( ) allows us to enter data from keyboard that will be formatted in a certain way.
The general form of scanf( ) statement is as follows:
scanf ( "format string", list of addresses of variables ) ;
For example:
scanf ( "%d %f %c", &c, &a, &ch ) ;

Note that we are sending addresses of variables (addresses are obtained by using ‘&’ the ‘address of’ operator) to scanf( ) function. This is necessary because the values received from keyboard must be dropped into variables corresponding to these addresses. The values that are supplied through the keyboard must be separated by either blank(s), tab(s), or newline(s). Do not include these escape sequences in the format string.
All the format specifications that we learnt in printf( ) function are applicable to scanf( ) function as well.

No comments:

Post a Comment