Skip to content

Modularizing C Program into multiple files

Modularizing a C program into multiple files is an essential practice for developing large, maintainable, and scalable software projects. This approach divides the program’s functionality into separate files, usually aligning with the concept of modularity and separation of concerns. Let’s delve into the importance of this structuring and illustrate it with examples and code snippets.

Importance of Modularizing

  1. Modularity: Breaking down a program into multiple files allows you to isolate different parts of your program into modules. Each module can focus on a specific task, making the program easier to understand, develop, and test.
  2. Reusability: Functions or definitions placed in separate files can be easily reused in other projects. This avoids duplication of code and promotes a DRY (Don’t Repeat Yourself) principle.
  3. Maintainability: With a well-structured project, it becomes easier to maintain and update the code. You can update or fix a module without affecting the rest of the program, provided the interfaces between modules remain unchanged.
  4. Collaboration: In team projects, having separate files allows multiple developers to work on different parts of the program simultaneously without creating conflicts.
  5. Compiling Efficiency: When a project is split into multiple files, only the modified files need to be recompiled, which can significantly speed up the development process for large projects.

Example and Code of Modularizing

Let’s illustrate this with a simple example: a program that calculates the area and perimeter of a rectangle and a circle. One way to do it would be by writing all the functions in one file as such:

#include <stdio.h>

// Function declarations
double calcRectArea(double width, double height);
double calcRectPerimeter(double width, double height);

// Function declarations
double calcCircleArea(double radius);
double calcCirclePerimeter(double radius);


int main() {
    double width = 5.0;
    double height = 10.0;

    printf("Area of Rectangle: %.2f\n", calcRectArea(width, height));
    printf("Perimeter of Rectangle: %.2f\n", calcRectPerimeter(width, height));

    double radius = 4.0;

    printf("Area of Circle: %.2f\n", calcCircleArea(radius));
    printf("Perimeter of Circle: %.2f\n", calcCirclePerimeter(radius));

    return 0;
}

// Function to calculate the area of a circle
double calcCircleArea(double radius) {
    return (3.1416) * radius * radius;

}

// Function to calculate the perimeter of a circle
double calcCirclePerimeter(double radius) {
    return 2*(3.1416)*radius;
}

// Function to calculate the area of a rectangle
double calcRectArea(double width, double height) {
    return width * height;
}

// Function to calculate the perimeter of a rectangle
double calcRectPerimeter(double width, double height) {
    return 2 * (width + height);
}

 

This code works fine but there are a few problems with this:

  1. As the code grows, it will become difficult to work on it. Thus it is not very maintainable
  2. Not reuseable either. For every project where you would want to reuse it, you will have to remove the un-needed stuff
    1. Example, you want to reuse the circle code, you have to remove all the rectangle code and vice-versa

Another approach is to split the code into multiple files. You can split it according to functionality, like one file containing area calculations and another containing perimeter calculations.

I am splitting the files as circles and rectangles.

File Structure

  • main.c – Contains the main function and the program’s entry point.
  • rectangle.c – Contains the implementation of the rectangle’s functions.
  • rectangle.h – Contains the declaration of the rectangle’s functions and any necessary includes or constants.
  • circle.c – Contains the implementation of the circle’s functions.
  • circle.h – Contains the declaration of the circle’s functions and any necessary includes or constants.

rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

// Function declarations
double calcRectArea(double width, double height);
double calcRectPerimeter(double width, double height);

#endif

rectangle.c

#include "rectangle.h"

// Function to calculate the area of a rectangle
double calcRectArea(double width, double height) {
    return width * height;
}

// Function to calculate the perimeter of a rectangle
double calcRectPerimeter(double width, double height) {
    return 2 * (width + height);
}

circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

// Function declarations
double calcCircleArea(double radius);
double calcCirclePerimeter(double radius);

#endif

circle.c

#include "circle.h"

// Function to calculate the area of a circle
double calcCircleArea(double radius) {
    return (3.1416) * radius * radius;

}

// Function to calculate the perimeter of a circle
double calcCirclePerimeter(double radius) {
    return 2*(3.1416)*radius;
}

main.c

#include <stdio.h>
#include "rectangle.h"
#include "circle.h"

int main() {
    double width = 5.0;
    double height = 10.0;

    printf("Area of Rectangle: %.2f\n", calcRectArea(width, height));
    printf("Perimeter of Rectangle: %.2f\n", calcRectPerimeter(width, height));

    double radius = 2.0;

    printf("Area of Circle: %.2f\n", calcCircleArea(width, height)); 
    printf("Perimeter of Circle: %.2f\n", calcCirclePerimeter(width, height));

    return 0;
}

Compiling and Linking

To compile and link the program, you would use commands like the following, assuming you are using GCC:

gcc -c main.c -o main.o
gcc -c rectangle.c -o rectangle.o
gcc main.o rectangle.o -o myprogram

This will create an executable named myprogram. The -c option tells GCC to compile the source files to object files (main.o and rectangle.o), and the final command links these object files into a single executable.

By structuring your program into multiple files in this way, you not only make your code more organized and maintainable but also leverage the benefits of modularity and reusability.

Leave a Reply

Your email address will not be published. Required fields are marked *