Tuesday, April 3, 2012

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.

0 comments:

Post a Comment