Basic Structure of a C Program
C
language is very popular language among all the languages. Sometimes I think
that had there been no "C" language there would have no C++ and even
Java. So let me explain you the basic structure of a C language.
The
structure of a C program is a protocol (rules) to the programmer, while writing
a C program. The general basic structure of C program is shown in the figure
below. The whole program is controlled within main ( ) along with left brace
denoted by “{” and right braces denoted by “}”. If you need to declare local
variables and executable program structures are enclosed within “{” and “}” is
called the body of the main function. The main ( ) function can be preceded by
documentation, preprocessor statements and global declarations.
Documentation
Section
|
||||||
Pre-Process or
Link Section
|
||||||
Definition
Section
|
||||||
Global
Declaration Section
|
||||||
Main() Function Section
{
}
|
||||||
Subprogram Section
( User–Defined
functions)
|
· Documentations - The documentation section consist
of a set of comment lines giving the name of the program, another name and
other details, which the programmer would like to use later.
·
Link Section - The link section provides instructions to the compiler to link
function from the system library.
·
Preprocessor
Statements - The preprocessor statement begin with # symbol
and are also called the preprocessor directive. These statements instruct the
compiler to include C preprocessors such as header files and symbolic constants
before compiling the C program. Some of the preprocessor statements are listed
below.
Example:
Example:
Header Files:
#include<stdio.h>
#include
<math.h>
#include<stdlib.h>
#include
<conio.h>
Symbolic Constants:
#define
PI 3.1412
#define
TRUE 1
#define
FALSE 0
·
Global Declarations - The variables are declared before the main ( ) function as well as
user defined functions are called global variables. These global variables can
be accessed by all the user defined functions including main ( ) function.
·
The main ( ) function - Each and Every C program should contain only one main
( ). The C program execution starts with main ( ) function. No C program is
executed without the main function. The main ( ) function should be written in
small (lowercase) letters and it should not be terminated by semicolon. Main (
) executes user defined program statements, library functions and user defined
functions and all these statements should be enclosed within left and right
braces.
·
Braces - Every C program
should have a pair of curly braces ({, }). The left braces indicates the
beginning of the main ( ) function and the right braces indicates the end of
the main ( ) function. These braces can also be used to indicate the
user-defined functions beginning and ending. These two braces can also be used
in compound statements.
·
Local Declarations - The variable declaration is a part of C program and all the variables
are used in main ( ) function should be declared in the local declaration
section is called local variables. Not only variables, we can also declare
arrays, functions, pointers etc. These variables can also be initialized with
basic data types. For examples-
Main (
)
{
int
sum = 0;
int
x;
float
y;
}
Here, the variable sum is declared as
integer variable and it is initialized to zero. Other variables declared as int
and float and these variables inside any function are called local variables.
·
Program
statements
These statements are building blocks of a program. They represent
instructions to the computer to perform a specific task (operations). An
instruction may contain an input-output statements, arithmetic statements,
control statements, simple assignment statements and any other statements and
it also includes comments that are enclosed within /* and */ . The comment
statements are not compiled and executed and each executable statement should
be terminated with semicolon.
·
User defined
functions
These are subprograms, generally, a
subprogram is a function and these functions are written by the user are called
user ; defined functions. These functions are performed by user specific tasks
and this also contains set of program statements. They may be written before or
after a main () function and called within main () function. This is an
optional to the programmer.
Now, let us write a small program to display some message shown below.
Example:
/*Program to add and subtract two no.
Name of program Prog1.c
Author name is Ramesh
Date of creation 4/4/05 */
#include<stdio.h>
#define PI 3.1416
int
Add(int x, int y);
int
main()
{ int a,b,c;
a=10, b=5;
c=Add(a,b);
printf("Sum is : %d",c);
getchar();
return
0;
}
/* Function for Adding two numbers */
int Add(int x, int y)
{ int z;
z=x+y;
return
z;
}
Note the followings
·
C is
a case sensitive programming language. It means in C printf and Printf
will have different meanings.
·
C has
a free-form line structure. End of each C statement must be marked with a
semicolon.
·
Multiple
statements can be one the same line.
·
White
Spaces (i.e. tab space and space bar) are ignored.
·
Statements
can continue over multiple lines.
No comments:
Post a Comment