Structure of the C Program
Structure of the C Program
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify,
document, and understand in a particular format. C program must follow the below-mentioned
outline in order to successfully compile and execute. Debugging is easier in a well-structured C
program.
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned
below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
This section consists of the description of the program, the name of the program, and the creation
date and time of the program. It is specified at the start of the program in the form of comments.
Documentation can be represented as:
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and this will not
interfere with the given code. Basically, it gives an overview to the reader of the program.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple files is
inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered by the compiler, it is replaced
by the actual piece of defined code.
Example:
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables.
Variables and functions which are declared in this scope can be used anywhere in the program.
Example:
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this
section. Operations like declaration and execution are performed inside the curly braces of the main
program. The return type of the main() function can be int as well as void too. void() main tells the
compiler that the program will not return any value. The int main() tells the compiler that the
program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is shifted
to the called function whenever they are called from the main or outside the main() function. These
are specified as per the requirements of the programmer.
Example:
Sections Description
/**
* file: sum.c
* author: you It is the comment section and is part of the description section of the
* description: program code.
to find sum.
*/
int main() main() is the first function that is executed in the C program.
Sections Description
{…} These curly braces mark the beginning and end of the main function.
printf(“Sum: %d”,
printf() function is used to print the sum on the screen.
sum(y));
int sum(int y)
{ This is the subprogram section. It includes the user-defined functions
return y + X; that are called in the main() function.
}
Program Creation
What Is Compilation In C?
A Program can be written in any language (middle level or high level). This written program is called a
source program. The source program is to be converted to the machine language, which is called an
Object Program. Either an Interpreter or a compiler will do the activity.
Interpreter: An Interpreter reads only one line of a source program at a time and converts it to the
object codes. In case there are errors, the same will be indicated instantly. The Program written with
an Interpreter can easily be read and understood by the other users as well, so security is not
provided. Any one can modify the source code. Hence it is easier the compilers.
Interpreter Disadvantage: It consumes more time for converting a source program to an object
program.
Compilers: A Compiler reads the entire program and converts it to the object code. It Provides errors
not of one line but errors of the entire program. Only Error free programs are executed. It consumes
little time for converting a source program to an object program. When the program length for any
application is large, compilers are preferred.
EXECUTING THE PROGRAM
The following steps are essential in executing a program in C.
1. Creation of Program: The program should be written in C editor. The file name does not
necessarily include the extension .C. The user can also specify his/her own extension.
2. Compilation and linking a program: The source program statement should be translated into
the object program, which is suitable for execution by the computer. The translation is done
after correcting each statement. If there is no error compilation proceeds and the translated
program is stored in another file with the same file name with extension .OBJ, if errors are
there the program files and functions that are required by the program. For example, if the
programmer is using the Pow ( ) function , then the object code of this function should
brought from math.h library of the system and linked to the main() program.
3. Executing the Program: After the compilation, the executable object code will be loaded in
the computers main memory and the program is executed.
All the above steps can be performed using menu options of the editor.
DECISION STATMENTS
In practical applications there are a number of situations where one has to change the order of the
execution of the statements based on the conditions. This involves a decision-making condition to
see whether a particular condition is satisfied or Not. On the basis of applications it is essential
The if statement
The if-ELSE statement
The if-ELSE-if- ladder statement
The Switch () statement
The CASE statement
The decision making statement checks the given condition and the executes its sub-block. The
decision statement decides the statement to be executed after the success or failure of a given
condition.
1. The IF statement:
C language uses the keyword if to execute a set of command lines or one command line when the
logical condition is true. It has only one option. The set of command lines or command lines are
executed only when the logical condition is true.
Syntax:
If ( condition) /* no semi-colon*/
Statement;
The statement is executed only when the condition is true. In case the condition is false the compiler
skips the lines within the if block. The condition is always enclosed within a pair of parentheses. The
condition statement should not be terminated with semi-colon (;) The statements following the if
statement are normally enclosed with curly braces { }. The curly braces indicate the scope of the if
statement. The default scope is one statement. But it is a good practise to use curly braces even with
a single statement.
main ()
Int v;
clrscr ();
if (v<10)
2. The if-ELSE statement : We observed the execution of if statement in the previous program
it is observed that the if statement executes only when the condition following if is true. It
does nothing when the condition is false. The if-else statement takes care of true as well as
false conditions. it has two blocks one blocks. One block is for if and it is executed when the
condition is true. The other block is of else and it is executed when the condition is the
false.The Else statement cannot be used without if. No multiple else statements are allowed
with one if.
Syntax:
If (Expression is true)
Statement1
Statement2 if Block
Statement3
Else
Statement4
Statement6
Read the values of a,b,c through the keyboard , add them and after the addition check if it is in the
range of 100 and 200 or Not. Print separate message for each.
Main()
Int a,b,c,d;
Clsrscr();
d=a+b+c;
printf( “\n The sum is %d which is in between 100 & 200” d);
else
}
3. Nested if else statement
In this kind of statements number of logical conditions are checked for executing various
statements. if any logical condition is true the compiler executes the block followed by if
condition otherwise it skips and executes ELSE block. In if-else statement else block is executed
by default after failure of condition. In order to execute the ELSE block depending upon certain
condition we can add repetitively if statements in Else block. This kind of nesting will be
unlimited.
Syntax
If (Condition)
Statement1
Statement2 if Block
Else if (condition)
Statement3
Statement4 else Block
Else
Statement5
Statement6
From the above black following rules can be described for applying nested if else if statements.
b) if the condition is false control passes to else block where condition is again check with the if
statement. This process continues if there is no if statement in the last else block number.
c) If one of the If statement satisfies the condition, other nested else….if statement not be
executed.