0% found this document useful (0 votes)
19 views

Structure of A C Program Updated

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Structure of A C Program Updated

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Structure of C Program

The structure of a C program is a protocol (rules) to the programmer, which programmer has
to follow while writing a C program. A C program is divided into different sections. There are
five main sections to a basic c program.
The general basic structure of C program is shown in the figure below.
/* Documentation or comment section */
/* Link section */
/* Definition section */
/* Global declaration section */
main()
{
Local Declaration part
Executable part (statements)
}
/* subprograms */

Documentation Section
The documentation section is used for displaying any information about the program like the
name of the program, the details of the programmer, purpose of the program, time of coding
and description etc. It gives anyone reading the code the overview of the code. This section
should be enclosed within comment lines.
There are two types of comments in C language.
Single line comment starts with two forward slashes (i.e. //) and is automatically terminated
with the end of line.
Multi-line comment starts with /* and terminates with */. Multi-line comment is used when
multiple lines of text are to be commented out.
The statements in the documentation section are ignored by the compiler.
Link section: This part of the code is used to declare all the header files that will be used in
the program. This instruction prompts the compiler to link the header files with the system
libraries.
#include<stdio.h> is a preprocessor directive which includes standard input/output (i.e. stdio)
header (.h) file. This file is to be included if standard input/output functions like printf or scanf
are to be used in a program
The definition section: consists of macro definitions, defining constants etc.
#define X 25 //Macro
Global declaration section
All the global variable used are declared in this part. The user-defined functions are also
declared in this part of the code. Anything declared in the global declaration section is
accessible throughout the program, i.e. Accessible to all the functions in the program.
main function section (): Every C-programs needs to have the main function. Each main
function contains 2 parts. A declaration part and an Execution part. The declaration part is the
part where all the variables are declared. The execution part begins with the curly brackets and
ends with the curly close bracket. Both the declaration and execution part are inside the curly
braces.
Sub-program section: is optional and used when we require including user defined functions
in the program. All the user-defined functions are defined in this section of the program.
The general form of header of a function is:
[return_type] function_name([argument_list])
The terms enclosed within square brackets are optional and might not be present in the function
header.
Example 1: contains all sections of a structure of a program
// Documentation Section
/*
file: display.c
programmer: you
*/
// Link
#include <stdio.h>
// Definition
#define A 20 //Macros
// Global Declaration
int x=5;
void display() ; // function prototype
// Main() Function
main( )
{
int y = 55;
printf("%d\n%d",x,y);
printf("\n%d",A);
display();
return 0;

}
// Subprogram
void display()
{
printf("\ndisplay");
}
//2nd example contains only 2 sections
/* This program accepts a number & displays it to the user*/
#include <stdio.h>
main( )
{
int number;
printf("Please enter a number: " );
scanf("%d", &number );
printf("You entered %d", number );
return 0;
}

You might also like