Saturday, September 20, 2014

2.2 BASIC STRUCTURE OF C-PROGRAM

C program is a collection of one or more functions. Every function is a collection of statements and performs some specific tasks. The general structure of C program is:

                                              Fig 01: Basic Structure of C-Program

 
ü  Documentation Section
The documentation section consists of a set of comment lines, giving the name of the program, the author and other details, which the programmer would like to use later. The comments can be written in two ways:

a)      Multi-line comment
/* This is a
multi-line
comment */

b)      Single-line comment
// This is a single-line comment
 
ü  Link Section
·         The link section provides instructions to the compiler to link functions from the system library.
·         If we want to access the functions stored in the library, it is necessary to tell the compiler about the files to be accessed. This is achieved by using the preprocessor directive #include as follows:

#include<filename>

where filename is the name of the library file that contains the required function definition. Preprocessor directives are placed at the beginning of the program. Some of the examples are:

            #include<stdio.h>
            #include<conio.h>
            #include<math.h>

ü  Definition Section
·         The definition section defines all symbolic constants.
·         A #define is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon.
·         Symbolic constants are usually written in uppercase so that they are easily distinguished from lowercase variable names.
·         #define instructions are usually placed before the main( ) function.

e.g.   #define YEAR 2014
         #define  PI 3.14

ü  Global Declaration Section
·         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 that is outside of all the functions.
·         This section also declares all the user-defined functions. (Will study in Functions)

ü  main( ) Function Section
·         Every C program must have one main( ) function section. This section contains two parts, declaration part and executable part.
·         The declaration part declares all the variables used in the executable part.
·         There is at least one instruction in the executable part.
·         These two parts must appear between the opening and the closing braces.
·         The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function section is the logical end of the program.
·         All the statements in the declaration and executable parts end with a semicolon (;).

ü  Subprogram Section
The subprogram section contains all the user-defined functions that are called in the main functions. User-defined functions are generally placed immediately after the main function, although they may appear in any order.

Sample Program

Let us look at a simple code that would print the words “Welcome to C-Lab”.

// A simple C-Program by Himdweep Khurana
#include<stdio.h>
#include<conio.h>
void main()
{
               clrscr();  
               /* my first program in C */
               printf(“Welcome to C-Lab! \n”);
               getch();
}

 
Let us look various parts of the above program:

  1. The first line of the program is a single line comment, giving the description about the program as well as author’s name.
  2. The next two lines are #include <stdio.h> and #include <conio.h> which are preprocessor command(s) which tells a C compiler to include these files before going to actual compilation.
  3. The next line void main() is the main function where program execution begins.
  4. The next line clrscr() is a function available in C which causes the output(s) of previous programs to be deleted from the output screen. This function’s definition is written in the header file conio.h.
  5. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program.
  6. The next line printf(...) is another function available in C which causes the message "Welcome to C-Lab!" to be displayed on the screen.
  7. The last line getch() is an input function that accepts data of character type. Until this line is executed, the program will not terminate.  
Basic rules applicable to all C programs:

1)      Each instruction in a C program is written as a separate statement.
2)      The statements in a program must appear in the same order in which we wish them to be executed.
3)      Blank spaces may be inserted between two words to improve the readability of the statement.
4)      All statements are entered in small case letters.
5)      Every C statement must end with a semicolon (;). Thus ; acts as a statement terminator.

Compile & Execute C Program:

Lets look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:

  1. Open a text editor and add the above mentioned code.
  2. Save the file as welcome.c
  3. Compile the program.
  4. In case there are syntax errors, remove them.
  5. Once the errors are removed, run the program.
  6. You will be able to see "Welcome to C-Lab" printed on the screen
Welcome to C-Lab!

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.