Tuesday, 18 August 2015

Structure Of A C Program

Every C Program contains a number of building blocks known as functions. Each function of it perform a specific task independently.

(i) Include Header File Section – C Program depends upon some header files for function definition that are used in the program. Each header file has extension ‘.h’.
The header files are included at the beginning of the program in the C language. These files should be included using #include directive as given below.

Example:
#include<stdio.h> or
#include “stdio.h”

In this example, <stdio.h> file is included, i.e. all the definitions and prototypes of functions and prototypes of function defined in this file are available in the current program.

(ii) Global Declaration – This section declares some variables that are used in more than one function. These variables are known as global variables. This section must be declared outside of all functions.

(iii) Function main() – Every program written in C must contain main() and its execution starts at the beginning of this function. In ASCII C standard, first line of C program from where program execution begins is written as follows.

int main(void)
This is the function definition for main(). Parenthesis followed to main is to tell the user again that main() is a function. The int main(void) is a function that takes no arguments and returns a value of type int. Here in this line int and void are keywords and they have special meanings assigned by the compiler. In case int is not mentioned in the above statement, by default the function returns an integer.

Alternately, one can also write the first line of C program from where program execution begins as follows,

void main(void)

Here, this function takes no arguments and returns nothing. Alternately, one can also write as follows.

void main() : This function returns nothing and takes no arguments.

In maximum programming examples the main function is written as void main(). This procedure is followed only to avoid writing return statement at the end of each program. This step helps to minimize source code lines. The programmer can either write the function main with int main(void) or void main().

(iv) Declaration Part – The declaration part declares the entire local variables that are used in executable part. Local variable scope is limited to that function where the local variables are declared. The initializations of variables can also be done in this section. The initialization means providing initial value to the variables.

(v) Executable Part – This part contains the statements following the declaration of the variables. This part contains a set of statements or a single statement.

(vi) User-defined Function – The functions defined by the user are called user-defined functions. These functions are defined outside the main() function.

(vii) Body of the Function – The statements enclosed within the body of the function (between opening and closing brace) are called body of the function.

(viii) Comments – Comments are not necessary in a program. However, to understand the flow the program a programmer can insert comments in the program. Comments are to be inserted by the programmer is useful for documentation. The clarity of the program can be followed if it is properly documented.

Comments are statement that give us information about the program which are to be placed between the delimiters /* and */. The programmers uses in the program for enhancing the lucidity frequently use comments. The compiler does not execute comments. Thus, we can say that comments are not a part of executable program.

Example:
/* This is single comment */
/* This is an example of  /* nested comments */.

No comments:

Post a Comment