structure of c program with example
Structure of C program
A C program is divided
into six different sections. Those are as follows.
The
six sections are,
- Documentation
- Link
- Definition
- Global Declarations
- Main functions
- Subprograms
Now let us learn about each of this layer in
detail.
Documentation Section
The
documentation section is the part of the program where the programmer gives the
details connected with the program. He usually gives the name of the program,
the details of the author and other details like the time of coding and
description. It gives overview of the code.
Example
/*
File
Name: Helloworld.c
*/
Normally
documentation writes in a comment.
Comment
There are two types of comment in c language. Comment
parts are not executed by the c program it means comment part is not displayed
in the output.
1) single line comment
2) multi line comment
Single Line
Comments
Single line comments
are produced by double slash \\.
Example
Example
#include<stdio.h>
void main(){
//printing information
printf("Hello C");
}
Multi Line
Comments
Multi-Line comments are produced by slash asterisk
\* ... *\. It can't be nested.
Syntax:
/*
code
to be commented
*/
Example
#include<stdio.h>
void main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
}
Link Section
This
part of the code is used to declare all the header files that will be used in
the program. This leads to the compiler being told to link the header files to
the system libraries.
Example
#include<stdio.h>
|
Definition Section
In
this section, we define different constants. The keyword define is used in this
part.
#define
PI=3.14 |
Global Declaration
Section
This
part of the code is the part where the global variables are declared. All the
global variable used are declared in this part. The user-defined functions are
also declared in this part of the code.
float
area(float r); int a=7; |
Main Function
Section
Every
C-programs needs to have the main function. Each main function has 2
parts. A declaration part and an Execution part. In the declaration part we declared all the variables. The execution part start with the
curly brackets and ends with the curly close bracket. Declaration and
execution parts are declared inside the curly braces.
void main() { int a=5; printf(" %d", a); }
|
Sub Program Section
User-defined functions are defined in this section of the program.
int add(int a, int b) { return a+b; }
|
Simple c Program
#include <stdio.h>
int main()
{
printf("hello,
world\n");
return 0;
}
Output
hello world