[go: up one dir, main page]

0% found this document useful (0 votes)
93 views4 pages

Struct of CProg

The document describes the basic structure of a C program, which consists of 6 main sections: documentation, link, definition, global declarations, main function, and subprograms. It provides examples of code for each section, and explains their purpose. The last part provides a full sample C program that calculates the area of a circle using some of these sections.

Uploaded by

Evariste Migabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views4 pages

Struct of CProg

The document describes the basic structure of a C program, which consists of 6 main sections: documentation, link, definition, global declarations, main function, and subprograms. It provides examples of code for each section, and explains their purpose. The last part provides a full sample C program that calculates the area of a circle using some of these sections.

Uploaded by

Evariste Migabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Structure Of C Program

A C program is divided into different sections. There are six main sections to a basic
c program.

The six sections are,

 Documentation
 Link
 Definition
 Global Declarations
 Main functions
 Subprograms

The whole code follows this outline. Each code has a similar outline.

Figure: Basic Structure of C Program

Documentation Section
The documentation section is the part of the program where the programmer gives
the details associated 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 anyone reading the code the overview of the code.

Example

/**

* File Name: Helloworld.c


* Author: Manthan Naik

* date: 09/08/2019

* description: a program to display hello world

*              no input needed

*/

Moving on to the next bit of this basic structure of a C program article,

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

1#include<stdio.h>
Moving on to the next bit of this basic structure of a C program article,

Definition Section
In this section, we define different constants. The keyword define is used in this part.

1#define PI=3.14
Moving on to the next bit of this basic structure of a C program article,

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.

1float area(float r);


2int a=7;
Moving on to the next bit of this basic structure of a C program article,

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.
1int main(void)
2{
3int a=10;
4printf(" %d", a);
5return 0;
6}
Moving on to the next bit of this basic structure of a C program article,

Sub Program Section


All the user-defined functions are defined in this section of the program.

1int add(int a, int b)


2{
3return a+b;
4}
Sample Program
The C program here will find the area of a circle using a user-defined function and a
global variable pi holding the value of pi

1
2/* File Name: areaofcircle.c
3 Author: Manthan Naik
4 date: 09/08/2019
5 description: a program to calculate area of circle
6*user enters the radius
7**/
8#include<stdio.h>//link section
9#define pi 3.14;//definition section
10float area(float r);//global declaration
11int main()//main function
12{
13float r;
printf(" Enter the radius:n");
14
scanf("%f",&r);
15printf("the area is: %f",area(r));
16return 0;
17}
18float area(float r)
19{
20return pi * r * r;//sub program
21}
Output
 

You might also like