Tuesday, October 12, 2010

Structure of a C Program with an example


STRUCTURE OF A C PROGRAM:
The basic structure of a C program is as shown below –
Documentation Section
----------------------------------------------
Link Section                                   
----------------------------------------------
Definition Section
----------------------------------------------
Global Declaration Section
main() Function Section
{
           Declaration Part
           ------------------------
           Executable Part
}
Subprogram section
            Function 1
            Function 2
                    .
                    .
                    .
            Function n

Description:
·         The documentation section consists of a set of comment lines giving the name of the program, the author’s name and other details, which the programmer would like to use later.
·         The link section provides instructions to the compiler to link functions from the system library.
·         The definition section allows symbolic constants.
·         There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section.
·         Every C program must have one main() function section.
o   This section contains two parts declaration part and executable part.
o   The declaration part declares all the variables used in the executable part.
o   There should be at least one statement in the executable part.
o   These two parts must appear between the opening and  the closing braces.
o   The program execution begins at the opening brace and ends at the closing brace.
o   The closing brace of the main function section is logical end of the program.
·         All statements in the declaration and executable parts end with a semicolon.
·         The subprogram section contains all  the user-defined functions that are called in the main function. The main function is very important compared to other sections.

Example:
main()
{
            /* Printing Begins */
            printf(“Hello World!\n”);
            /* Printing Ends */
}
This program displays Hello World! on the screen.
Description-
·         The first line is where the execution begins. There must be exactly one main() in every program.
o   The empty brackets () indicate that the function does not have any arguments.
o   ‘main’ can be written in any of the following ways –
main()                        int main()                               void main()  
main(void)                int main(void)                      void main(void)
o   Here int means main returns an integer and if void returns nothing.

·         The function is followed by { indicating the beginning of function body.
·         The first line inside ‘main’ function is a comment line (which begins with /* and ends with */). Such comment lines are used to increase the readability of a program.
o   Comments are non-executable.
o   They cannot be nested                   /*...... /* ....... */ ....... */ is not valid.
·         The second line has a function printf, which is a predefined C function for printing output.
o   This function causes everything between the starting and ending quotation marks to be printed out.
·         The \n is a newline character, which instructs the computer to go to next (new) line. It is similar to the return key of the keyboard.
o   No space is allowed between \ and n.
·         ‘printf’ function is defined under the pre-processor directive stdio.h
o   ‘stdio.h’ is a header file used for calling functions for input and output.



READING DATA FROM KEYBOARD:
Data can be inputted and stored into a variable using a predefined function called scanf.
·         ‘scanf’ is also a part of the directive ‘stdio.h’.
·         Its syntax is –
scanf(“control string”, &variable1, &variable2, ...);
Example: -                 scanf(“%d”, &n);
While executing a program, this statement makes the cursor to blink on the screen until an integer is inputted from the keyboard and return key is pressed.

After an integer is keyed in, that entered value will be saved in the variable n, so that keyed in value can be accessed later using the variable ‘n’.
·         Thus scanf makes the program interactive and makes it user friendly.

No comments:

Post a Comment