Tuesday, April 3, 2012

Escape Sequences in C Programming Language

Let’s have a look at a simple C program:


#include
main ( )
{
printf ("This is easy !!\n");
}


You might be wondering about the \n (pronounced backslash n) in the string argument of the function printf ( ):

"This is easy!!\n"

The \n is an example of an escape sequence: it's used to print the newline character. If You have executed Programs 1.1 or 1.2 you will have noticed that the \n doesn't appear in the output Each \n in the string argument of a printf ()causes the cursor to be placed at the beginning of die next line of output. Think of an escape sequence as a "substitute character" for printing hard-to-get characters. In an earlier section we learnt that s are not allowed in strings., but including \n's within one makes it easy to output it in several lines: if you wish to print a string in two or more lines, place the \n wherever you want to insert a new line in the output, as in the example below:

main ( )
{
printf ("This\nstring\nwill\nbe\nprinted\,nin\n9\nlines.\n");
}

Each \n will force the cursor to be positioned at the beginning of the next line on the screen, from where further printing will begin. Here is part of the output from executing the above program:

This
string
will
etc.

If a string does not contain a \n at its end, the next string to be printed will begin in the same line as the last, at the current position of the cursor, i.e. alongside the first string.

We have seen that the "double quotes" character is used to mark the beginning and end of a C string. How then can the "double quotes" character itself be included within a string? This time the escape sequence \" comes to our aid: it prints as “ in the output. Similarly, the escape sequence, \\ helps print \, and \' the single quote character,'. All escape sequences in C begin with a backslash.

Be careful to remember that "" is not the escape sequence for the "double quote" character (as it is in some versions of BASIC). In C "" is the null string., the null string is not "empty ", as one might have thought; it contains a single character, the null character, ASCII 0 (in which all bits are set to zero). We will see later that the last character of every C string is the (invisible) null character. Its presence helps the computer determine the actual end of the string.

Other escape sequences available in C are:

\a

ring terminal bell (the a is for alert) [ANSI] extension]

\?

question mark [ANSI extension]

\b

Backspace

\r

carriage return

\f

Formfeed

\t

horizontal tab

\V

vertical tab

\O

ASCII null character

Placing any of these within a string causes either the indicated action, or the related character to be output.

A Simple C Program

The best way to learn C or any programming language is to begin writing programs in it; so here's our first C program:

# include <stdio.h>
main()
{
     printf ("This is easy !!\n");
}

There are a few important points to note about this program, points which you will find common to all C programs.

Programs in C consist of functions, one of which must be main (). When a C program is executed, main ( ) is where the action starts. Then other functions may be "invoked". Here you can ask a very basic question that what a function is? A function is a sub-program that contains instructions or statements to perform a specific computation on its variables. When its instructions have been executed, the function returns control to the calling program, to which it may optionally he made to return the results of its computations. Because main ( ) is a function, too, from which control returns back to the operating system at program termination, in ANSI C it is customary, although not required, to include a statement in main ( ) which explicitly returns control to the operating environment. In ANSI C the above program is correctly written as follows:

/*ANSI C Version */ 
#include <stdio.h>
void main (void)
{
    printf ("This is easy!!\n");
    return 0;
}

We'll learn more about functions as we go along our C programming tutorial, but for now you may recognize them by the presence of parentheses after their names.

Apart from main ( ), another function in the program above is the printf ( ). printf ( ) is an example of a "library function". It's provided by the compiler, ready for you to use. In addition to its variables, a function, including main ( ), may optionally have arguments, which are listed in the parentheses following the function name. The arguments of functions are somewhat different from the arguments that people have: they are values of the parameters in terms of which the function is defined, and are passed to it by the calling program. The instructions which comprise a function are listed in terms of its parameters and its variables.

In the example above, main ( ) has no arguments. printf ( ) has one: it's the bunch (more properly: string) of characters enclosed in double quotes:
"This is easy!!\n"

When the printf ( ) is executed, its built-in instructions process this argument. The result is that the string is displayed on the output device, which we will usually assume is a terminal. The output of the program will be:
This is easy!!

With the cursor stationed at the beginning of the next line on the screen. In C a string is not a piece of thread: it's any sequence of characters enclosed in double quotes. A string must not include a "hard" carriage-return (), i.e. all its characters must be keyed in on a single line on the terminal.

"This is most certainly not a C string. It contains a carriage return character."

As we have seen in the case of printf () when a function is invoked, it is passed (the values of ) its arguments. It then executes its instructions, using these values. On completion the function returns control to the module from which it was invoked. The nice thing about a function is that it may be called as often as required in a program, to perform the same computational steps on the different sets of values that are passed to it. Thus there can be several invocations to printf ( ) within a program, with different string arguments each time.