Modular Programming in C

by Sanjay Mishra

Why is modular programming important?

As programs grow in size, it becomes important to break them into separate parts (modules) that communicate with rest of the program through a few well defined interfaces.

If we decompose the program into modules well is we can code each module independently. Also if change happens we can localize changes to a particular module without impacting the rest of the programs.

For example, as is their habit, a LCD which we had used in a product went obsolete. We had a written the hardware abstraction layer for the LCD as a separate module. We substituted a different LCD, and modified the LCD hardware abstraction module while not impacting rest of the program.

How to achieve modular programming in C?

C provides two keywords static and extern that facilitate this. Disciplined coding practice also aids modularization.

The key is to have a header file for each module. This header file contains

  • a comment section about the module
  • definition of all constants publicly exposed by the module
  • definition of all structures publicly exposed by the module
  • all publicly exposed variables defined with extern
  • all publicly exposed functions defined with extern

In the header file you should only put definitions of constants, structures, variables and functions that you want to expose.

As a coding practice I prefix all constant, structure, variable and function definitions with the name of the module. This makes it easy to identify the source of definition in a larger program with many modules.

The implementation file(s) contains the actual definition of variables and implementations of the functions defined in the header file.

The implementation file also contains definitions of those variables, structures and constants that are used only in the implementation file. It also contains definitions and implementations of helper functions. These should be declared as static to prevent access from outside. I all find it a good programming practice to prefix all internal names with "_".


Header file for a module named node node.h


  Interface for node module

  node_var1 is a variable used inside the module

  node_do_something() is a function in the module


extern int node_var1;

extern int node_do_something(void);

Implementation file for module named node node.c

#include "node.h"

int node_var1;

static int _node_var2;

static void _node_helper_function(void){


int node_do_something(void){

  node_var1 = 2;

  _node_var2 =35;



The main file which uses module node

#include "node.h"

int main(int argc, char *argv[]){





What's next?

We can extend the modular programming concepts explained on this page to do OOP in C.

Top of Modular Programming

Need More Help? Have An Opinion?

Do you need more help to solve your problem? Would you like to ask the author a question about your specific problem? Do you have a great idea about this?

We will post an answer within 2 business days. If you need more immediate assistance or you would like to discuss your issue privately, please use our contact us form or call us at 1-888-215-8557. We love solving technical issues and there is no charge if we solve your problem over email or over a short phone call.

[ ? ]

Author Information (optional)

To receive credit as the author, enter your information below.

(first or full name)

(e.g., City, State, Country)

Submit Your Contribution

  •  submission guidelines.

(You can preview and edit on the next page)