Skip to content
StudyFame

Structure of a C Program

Every C program is built in the same order, like a letter has a fixed format: address, date, subject, body, signature.

"Explain the basic structure of a C program with an example" is one of the most common exam questions. In this lesson you will learn its 6 sections, what goes in each one, and a complete example that uses all of them.

What You Will Learn

  • The 6 sections of a C program, in the correct order.
  • What each section does, in one line.
  • Which sections are compulsory and which are optional.
  • A full program that uses every section.

Structure of a C Program Explained

Exam definition: A C program is divided into six sections: Documentation, Link, Definition, Global Declaration, main() function, and Subprograms. Only the main() function is compulsory.

#SectionWhat it containsExample
1DocumentationComments about the program: name, author, date, purpose/* Area of circle */
2LinkHeader files the program needs#include <stdio.h>
3DefinitionSymbolic constants (macros)#define PI 3.14159
4Global declarationGlobal variables and function declarations, usable everywherefloat area(float r);
5main() functionWhere the program starts. Has a declaration part and an executable partint main(void) { ... }
6SubprogramsUser-defined functions called from main()float area(float r) { ... }

Memory trick: "Doctor Likes Drinking Green Mango Shakes" = Documentation, Link, Definition, Global, Main, Subprograms.

Compulsory or optional?

  • Compulsory: only the main() function. A program cannot run without it.
  • Almost always needed: the Link section, because we need stdio.h for printf().
  • Optional: Documentation, Definition, Global declaration and Subprograms.

The two parts inside main()

  • Declaration part: where variables are created, e.g. float radius;
  • Executable part: the statements that do the work, e.g. printf(...)

Syntax: General Structure of a C Program

/* 1. Documentation section: comments about the program */

#include <stdio.h>           /* 2. Link section */

#define NAME value           /* 3. Definition section */

int global_variable;         /* 4. Global declaration section */
void function_name(void);

int main(void)               /* 5. main() function section */
{
    /* declaration part */
    /* executable part */
    return 0;
}

void function_name(void)     /* 6. Subprogram section */
{
    /* statements */
}

Example 1: C Program Showing All 6 Sections

This program finds the area of a circle. Each section is marked with a comment.

/* Program: Area of a circle
   Author : StudyFame
   Purpose: Show the structure of a C program */   /* 1. Documentation */

#include <stdio.h>                                  /* 2. Link */

#define PI 3.14159                                  /* 3. Definition */

float area(float r);                                /* 4. Global declaration */

int main(void)                                      /* 5. main() */
{
    float radius = 5.0f;                            /* declaration part */

    printf("Radius = %.1f\n", radius);              /* executable part */
    printf("Area of circle = %.2f\n", area(radius));

    return 0;
}

float area(float r)                                 /* 6. Subprogram */
{
    return PI * r * r;
}

Output

Radius = 5.0
Area of circle = 78.54

Code Explanation

  1. Documentation: the comment at the top tells the reader what the program does. The compiler ignores it.
  2. Link: #include <stdio.h> brings in printf().
  3. Definition: #define PI 3.14159 means "wherever you see PI, use 3.14159".
  4. Global declaration: float area(float r); tells the compiler that a function named area exists. It takes a float and returns a float.
  5. main(): creates radius with value 5, prints it, then calls area(radius) and prints the answer.
  6. Subprogram: area() calculates π × r × r = 3.14159 × 5 × 5 = 78.53975, which prints as 78.54 because of %.2f.

How It Works

  1. The preprocessor runs first: it adds stdio.h, replaces PI with 3.14159 and removes the comments.
  2. The compiler reads the global declaration, so it already knows about area() when main() calls it.
  3. When the program runs, execution starts at main(), not at the top of the file.
  4. main() calls area(). Control jumps to area(), calculates the result, and comes back to main() with the answer.
  5. return 0; ends the program.

Example 2: The Smallest Valid C Program

Only main() is compulsory, so this is a complete, valid C program:

int main(void)
{
    return 0;
}

Output

(nothing is printed)

It compiles and runs, but does nothing. As soon as you want to print something, you need the Link section (#include <stdio.h>).

Important Points to Remember

  • A C program has 6 sections.
  • Only main() is compulsory.
  • Execution always starts from main(), wherever it is written in the file.
  • main() has two parts: declaration and executable.
  • Lines starting with # (Link and Definition) do not end with a semicolon.

Common Mistakes Beginners Make

MistakeWhat happensFix
Calling a function before declaring itError or warning: implicit declaration of functionDeclare it in the global section, or write it above main()
#define PI 3.14; (semicolon)The ; becomes part of PI and breaks expressionsNo semicolon after #define
Writing two main() functionsError: redefinition of 'main'Only one main() per program
Putting #include after main()printf is not known inside main()Header files go at the top

Structure of C Program Practice Questions

  1. Name the 6 sections of a C program in order.
  2. Which section is compulsory?
  3. What is written in the documentation section?
  4. What is the difference between the link section and the definition section?
  5. What are the two parts of the main() function?
Show answers
  1. Documentation, Link, Definition, Global declaration, main(), Subprograms.
  2. Only the main() function.
  3. Comments about the program: name, author, date and purpose.
  4. The link section includes header files (#include). The definition section creates symbolic constants (#define).
  5. The declaration part and the executable part.

Coding Exercise

Write a program with all 6 sections that calculates the area of a rectangle. Use #define for the unit text ("sq cm"), and keep a global variable int count = 0; that your function increases each time it is called. Print the area for length 8 and width 5.

Show solution
/* Program: Area of a rectangle (6 sections) */   /* Documentation */

#include <stdio.h>                                /* Link */

#define UNIT "sq cm"                              /* Definition */

int count = 0;                                    /* Global declaration */
int area(int length, int width);

int main(void)                                    /* main() */
{
    int result = area(8, 5);
    printf("Area = %d %s\n", result, UNIT);
    printf("area() was called %d time\n", count);
    return 0;
}

int area(int length, int width)                   /* Subprogram */
{
    count = count + 1;
    return length * width;
}
Area = 40 sq cm
area() was called 1 time

Quick Quiz: Structure of a C Program (MCQ)

  1. Which section of a C program is compulsory?

    A) Documentation   B) Link   C) main() function   D) Subprograms

  2. #include <stdio.h> belongs to which section?

    A) Definition   B) Link   C) Global declaration   D) Documentation

  3. #define PI 3.14 belongs to which section?

    A) Definition   B) Link   C) main()   D) Subprograms

  4. Program execution starts from:

    A) The first line   B) The documentation section   C) main()   D) The last function

  5. User-defined functions are written in which section?

    A) Link   B) Definition   C) Subprograms   D) Documentation

Show answers
  1. C: the main() function.
  2. B: the Link section.
  3. A: the Definition section.
  4. C: main().
  5. C: the Subprograms section.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Number of sections6
OrderDocumentation → Link → Definition → Global → main() → Subprograms
Compulsory sectionOnly main()
Link section#include header files
Definition section#define constants
Parts of main()Declaration part + executable part
Execution starts atmain()

Frequently asked questions

Which section of a C program is compulsory?
Only the main() function. All other sections are optional.
What is the use of the global declaration section?
It declares global variables and functions that can be used anywhere in the program.
Does a C program start from the first line?
No. It always starts from main().
Why don't #include and #define end with a semicolon?
They are preprocessor directives, not C statements.

Remember

  • Order trick: D-L-D-G-M-S ("Doctor Likes Drinking Green Mango Shakes").
  • Only main() is compulsory.

Next Topic

Next lesson: How C Program Compilation Works. You will see the 4 steps your code goes through, and the files the compiler creates at each step.