Structure of a C program

A typical C program is structured into various sections, each serving a specific purpose. Here's a basic outline of the structure of a C program:

  1. Documentation Section (Optional):

    • Comments providing information about the program, including the name of the program, its purpose, the author, and other relevant details. While not mandatory, this section enhances code readability and understanding.
     * Program: MyCProgram
     * Author: Your Name
     * Purpose: Brief description of the program.
  1. Header Files:

    • Include statements for header files that provide necessary declarations for functions used in the program. Commonly used headers include <stdio.h> for input and output functions and <stdlib.h> for functions involving memory allocation and other utilities.
    #include <stdio.h>
    #include <stdlib.h>
  1. Constants and Macros (Optional):

    • Definition of constants and macros that are used throughout the program. This section can enhance code readability and maintainability.
    #define MAX_SIZE 100
  1. Global Declarations (Optional):

    • Declarations of global variables and functions that will be used throughout the program. It's generally good practice to minimize the use of global variables.
    int globalVariable;

    void myFunction();
  1. Main Function:

    • The main function is the starting point of execution. It contains the code that will be executed when the program is run.
    int main() {
        // Program logic goes here

        return 0;
  1. Function Definitions:

    • Definitions of user-defined functions. These can be placed either before or after the main function, depending on whether they are used before or after their definition.
    void myFunction() {
        // Function logic
  1. Additional Functions and Sections (Optional):

    • Additional functions and sections as needed, depending on the complexity of the program.
  2. Preprocessor Directives (Optional):

    • Additional preprocessor directives, such as conditional compilation statements or file inclusion.
    #ifdef DEBUG
    // Debugging-related code

Remember that this is a basic template, and the structure may vary based on the complexity and requirements of the program. The core logic typically resides within the main function, and additional functions are used to modularize the code for better readability and maintainability.

Let's create a simple C program that calculates the sum of two numbers. I'll provide a commented explanation for each section of the program.

 * Program: SimpleSumCalculator
 * Author: Jyotiprakash Mishra
 * Purpose: Calculate the sum of two numbers.

#include <stdio.h>

// Constants and Macros
#define MAX_SIZE 100

// Function Declarations
int addNumbers(int a, int b);

int main() {
    // Variable Declarations
    int num1, num2, sum;

    // Input
    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    // Calculation
    sum = addNumbers(num1, num2);

    // Output
    printf("Sum: %d\n", sum);

    return 0;

// Function Definitions
int addNumbers(int a, int b) {
    return a + b;


  1. Documentation Section: Provides information about the program, including its name, author, and purpose. This section is optional but enhances code readability and understanding.

  2. Header Files: Includes the necessary header file <stdio.h> for input and output functions.

  3. Constants and Macros: Defines a constant MAX_SIZE, although it is not used in this simple program.

  4. Function Declarations: Declares the function addNumbers before its usage in the main function.

  5. Main Function:

    • Variable Declarations: Declares variables num1, num2, and sum to store user input and the result of the addition.

    • Input: Prompts the user to enter two numbers, reads them using scanf, and stores them in num1 and num2.

    • Calculation: Calls the addNumbers function to calculate the sum of num1 and num2.

    • Output: Prints the result using printf.

  6. Function Definitions: Defines the addNumbers function, which takes two integers as parameters and returns their sum.

This simple program demonstrates the basic structure of a C program, including user input, function usage, and output.