Lesson 02·C Programming

Structure of Program

The standard layout and organization of a C program

The Basic Structure of C Program

Structure Outline
/* [ Document Section ] */
/* [ Links Section ] */
/* [ Definition Section ] */
/* [ Global declaration section ] */

void main() {
    /* [ Variable declaration section ] */
    /* [ Function declaration section ] */
    /* [ Executable statements ] */
}

/* function definition 1 */
void func1() {
    /* --------------------- */
    /* --------------------- */
}

/* function definition 2 */
void func2() {
    /* --------------------- */
    /* --------------------- */
}

Explanation of Program Sections

  • Document Section: Contains set of comment lines giving name of the program, author, and details.
  • Links Section: Provides instructions to the compiler to link functions from system libraries (e.g., #include <stdio.h>).
  • Definition Section: Defines all symbolic constants using preprocessor directives (e.g., #define PI 3.14).
  • Global Declaration Section: Declares variables and user-defined functions that are accessible across the entire program.
  • main() Function: The mandatory entry point of execution for every C program containing declaration and executable parts.
  • Subprogram Section: Contains user-defined functions called by main() or other subprograms.

Example Program: Fibonacci Series

A complete practical program demonstrating the basic structural sections in action:

C Example
#include <stdio.h>

int main()
{
    int n1 = 0, n2 = 1, n3, i, number;

    printf("Enter the number of elements: ");
    scanf("%d", &number);

    printf("\n%d %d ", n1, n2); // printing 0 and 1

    /* for loop starts from 2 because 0 and 1 are already printed */
    for (i = 2; i < number; ++i)
    {
        n3 = n1 + n2;
        printf("%d ", n3);
        n1 = n2;
        n2 = n3;
    }

    return 0;
}
Program Output
Enter the number of elements: 8

0 1 1 2 3 5 8 13