Sie sind auf Seite 1von 3

Structure of a C Program

4.1. INTRODUCTION
As C is basically a programming language, let us go into the concepts of
programming. At this stage it may be somewhat difficult to understand but it is
easy if you understand the logic behind. A C program can be viewed as a group of
building blocks called functions. A function is a subroutine that may include one or
more statements designed to perform a specific task. To write a C program, we
first create functions and then put them together. A C program may contain one
or more sections shown in Fig below :
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() Function section
{
Declaration Part
Executable part
}
Subprogram Section
Function-
Function-!
" " "
Function-n
Let us see a sample program for calculating the sum of two integers and to
display its result :
/* Program to calculate the sum of two itegers */
! iclu"e #st"io.h$
! iclu"e #coio.h$
%oi" mai&'
(
it a) *) sum +
clrscr&' +
,ritf&-.ter the %alues for a a" * / -' +
scaf&-0" 0"-) 1a) 1*' +
sum 2 a 3 * +
,ritf&-4The sum of 0" a" 0" is 0"-) a) *) sum' +
getch&' +
4.2 Structure of a C Program
5
4.6. DOCU7.NT8TION S.CTION
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. Any comment used in the program must be enclosed within ! and !.
This may or may not present in the program as per the programmer"s wish. The
comment statement is not an e#ecutable statement. The compiler skips the line
when it encounters a comment statement. Comment statement may be included
in between the program in order to understand each line of the program.
4.9. :IN; S.CTION
The link section provides instruction to the compiler to link functions from the
system library. The second line in the program $ include %stdio.h& refers to a
special file which contains the information about the various standard input and
output operations, which must be included in the program while it is compiled.
The $ sign is called as the preprocessor directive and the stdio.h is a header file
which contains the information of all the standard input and output commands.
There are many header files like conio.h, math.h etc. The header file is usually
enclosed in % and &.
4.4. D.<INITION S.CTION
The definition section defines all symbolic constants.
4.=. >:O?8: D.C:8R8TION S.CTION
There are some variables that are used in more than one function. 'uch
variables are called global variables and are declared in the global declaration
section that is outside of all the functions.
4.@. mai&' <UNCTION S.CTION
(very C program must have one main)* function section. This section
contains two parts, declaration part and e#ecutable part. The declaration part
declares all the variables used in the e#ecutable part. A main)* function may or
may not contain a declaration statement but there is at least one statement in the
e#ecutable part. These two parts must appear between the opening and the
closing braces.
The program e#ecution 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 e#ecutable parts end with a
semicolon. There should be only one main)* function in a program.
Structure of a C Program 4.3
+n our program we have declared :
int a, b, sum ,
-here int is the data type in C and a,b and sum are the variables used in the
C program. The type of a variable informs the compiler how the variables are to
be stored i.e., how much of memory space is to be allocated.
4.A. SU?PRO>R87 S.CTION
The subprogram section contains all the user.defined functions that are called
in the main function. /ser.defined functions are generally placed immediately
after the main)* function, although the may appear in any order.
Note / All sections, e#cept the main function section may be absent when
they are not re0uired.
The above program displays the following output :
RUN 1 /
.ter the %alues for a a" * / = 1B
The sum of = a" 1B is 1=

Das könnte Ihnen auch gefallen