23ADT001 - C Programming
Module I
Topic – Structure of C Program
23ADT001 - C PROGRAMMING 1
BASIC STRUCTURE OF A C PROGRAM:
Include header file section
Global declaration section
Documentation section /*comments*/
Link Section main () /*function name*/
{
Definition Section
/*comments*/
Global declaration section //Declaration part
Function prototype declaration //Executable part
}
section
User defined functions
Main function
{
User defined function definition // statements
section }
23ADT001 - C PROGRAMMING 2
Sections Description
We can give comments about the program, creation or
modified date, author name etc in this section. The characters
or words or anything which are given between “/*” and “*/”,
Documentation
won’t be considered by C compiler for compilation
section
process.These will be ignored by C compiler during
compilation.
Example : /* comment line1 comment line2 comment 3 */
Header files that are required to execute a C program are included in this
Link Section
section
Definition
In this section, variables are defined and values are set to these variables.
Section
Global
Global variables are defined in this section. When a variable is to be used
declaration
throughout the program, can be defined in this section.
section
Function
prototype Function prototype gives many information about a function like return
declaration type, parameter names used inside the function.
section
Every C program is started from main function and this function contains
Main function
two major sections called declaration section and executable section.
User defined User can define their own functions in this section which perform
23ADT001 - C PROGRAMMING 3
function section particular task as per the user requirement.
/*
Documentation section
C programming basics & structure of C programs
Author: fresh2refresh.com
Date : 01/01/2012
*/
#include <stdio.h> /* Link section */
# define pi 3.14 //definition section
int total = 0; /* Global declaration section */
int sum (int, int); /* Function declaration section */
int main () /* Main function */
{
printf ("This is a C basic program \n");
total = sum (1, 1);
printf ("Sum of two numbers : %d \n", total);
return 0;
}
int sum (int a, int b) /* User defined function */
{
return a + b; /* definition section */
}
23ADT001 - C PROGRAMMING 4
C Basic commands Explanation
This is a preprocessor command that includes
#include <stdio.h> standard input output header file(stdio.h) from the C
library before compiling a C program
This is the main function from where execution of
int main() any C program begins.
{ This indicates the beginning of the main function.
whatever is given inside the command “/* */” in any
/
C program, won’t be considered for compilation and
*_some_comments_*/ execution.
printf(“Hello_World!
printf command prints the output onto the screen.
“);
This command waits for any character input from
getch(); keyboard.
This command terminates C program (main
return 0; function) and returns 0.
} This indicates the end of the main function.
23ADT001 - C PROGRAMMING 5
i) Include header file section
Each header file by default is extended with .h
Header file should be included using #include directive as
# include <stdio.h> or
#include “stdio.h”
ii) Global declaration
This section declares some variables that are used in more than one
function.
iii) main function:
Every program should have main function.
Empty parenthesis after main are necessary
Function main() is a starting point of every C program
The program execution begins with the opening brace “{“ and ends with a
closing brace “}”
iv) Declaration point:
Declares the entire variables that are used in executable part
Initialization of variables are also done in this section
23ADT001 - C PROGRAMMING 6
Structure of C Program
v) Executable part
This part includes the statements following the declaration of variables
vi) User defined function
Functions that are defined by user
Functions are defined before or after the main function
Comments
Comments are useful for documentation
// statements - single line comment
/* statements */ - multiple line comment
Compilation and Linking processes:
A program written in a high level language can run in two ways
• Compiled into a program in the native machine language and then run on the target machine
• Directly interpreted and the execution is simulated within an interpreter
23ADT001 - C PROGRAMMING 7
Example: Finding the area of a circle
Algorithm
Step1: Start
Step2: Read the value of r
Step3: Calculate area
Step4: Print area
Step5: Stop
23ADT001 - C PROGRAMMING 8
Pseudocode
Begin
READ r
COMPUTE area=3.14*r*r
PRINT area
stop
23ADT001 - C PROGRAMMING 9
Flowchart
START
Read r
area=3.14*r*r
Print area
STOP
23ADT001 - C PROGRAMMING 10
Program
/*
Documentation section
C programming basics & structure of C programs
#include <stdio.h> /* Link section */
# define pi 3.14 //definition section
int area = 0; /* Global declaration section */
int main () /* Main function */
{
int r; // Local variable declaration
printf (“Enter the radius of a circle \n");
scanf(“%d”, &r);
area = pi * r* r
printf (“Area of circle is : %d \n", area);
return 0;
}
23ADT001 - C PROGRAMMING 11
Try
• Area of Rectangle
• Simple interest
23ADT001 - C PROGRAMMING 12
Summary
• Basic structure of C Program
23ADT001 - C PROGRAMMING 13
References
• Ashok N.Kamthane, Amit.N.Kamthane, “Programming in C”, 3rd
Edition, Pearson Education, 2015
• Ajay Mittal, “Programming in C-A Practical Approach”, 3rd Edition,
Pearson Education, 2010.
• Yashavant P.Kanetkar, “Let Us C”, 16th Edition, BPB Publications,
2018.
• PradipDey, ManasGhosh, “Computer Fundamentals and
Programming in C”, 2nd Edition, Oxford University Press, 2013.
23ADT001 - C PROGRAMMING 14
23ADT001 - C PROGRAMMING 15