Structure of a C Program

Basic Structure of a C Program

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

The Six Sections Are:

  1. Documentation Section

  2. Include / Link / Preprocessor Section

  3. Definition Section

  4. Global Declarations Section (functions and variables)

  5. Main Function

  6. Subprogram Functions


Overview

Now let us learn about each of these sections in detail.





Figure: Basic Structure Of C Program


Sample Program

The C program below finds the area of a circle using a user-defined function and a global variable PI holding the value of π.

/* File Name: areaofcircle.c Author: Abhijith Date: 06/05/2021 Description: A program to calculate area of circle. User enters the radius. */ #include <stdio.h> // Link section #define PI 3.14 // Definition section float area(float r); // Global declaration /****************************/ int main() // Main function { float r; printf("Enter the radius:\n"); scanf("%f", &r); printf("The area is: %f", area(r)); return 0; } /****************************/ float area(float r) // Subprogram function { return PI * r * r; }

Sections of the Program

1. Documentation Section

The documentation section contains details about the program such as:

  • File name

  • Author name

  • Date of creation

  • Description of the program

It provides an overview for anyone reading the code.

/* File Name: areaofcircle.c Author: Abhijith Date: 06/05/2021 Description: A program to calculate area of circle. User enters the radius. */

2. Link Section

This section declares the header files used in the program.
It tells the compiler to include system libraries.

#include <stdio.h>

3. Definition Section

In this section, constants are defined using the #define directive.

#define PI 3.14

4. Global Declaration Section

This section declares:

  • Global variables

  • User-defined function prototypes

float area(float r); int a = 7;

5. Main Function Section

Every C program must have a main function.
It contains:

  • Declaration part → where variables are declared

  • Execution part → where statements are written

Both parts are enclosed within curly braces { }.

int main() { float r; printf("Enter the radius:\n"); scanf("%f", &r); printf("The area is: %f", area(r)); return 0; }

6. Subprogram Section

This section contains user-defined functions used in the program.

float area(float r) { return PI * r * r; }

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Single and Multi Dimensional Arrays