C Modular Programming

Advanced
6 min read
Updated: Nov 2026
1 example

What Is Modular Programming?

Modular programming divides code into modules. Each module has a header (.h) declaring its public interface and a source file (.c) with the implementation. Callers include the header only. Mark private helpers static so they do not leak into the global namespace.

Remember
/* gcd.h    */ int gcd(int a, int b);
/* gcd.c    */ #include "gcd.h"  int gcd(...) { ... }
/* main.c   */ #include "gcd.h"  ... gcd(48, 18) ...

gcc main.c gcd.c -o app

Cheat Sheet

Keep these facts handy while you work with Modular Programming in C.

TopicDetail
HeaderDeclarations and types
SourceDefinitions
Privatestatic functions
BuildCompile all .c files, link once

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Header Files.

Example 1 — A math module used by main

Build with gcc main.c mathops.c -o app.

C
/* mathops.h */
#ifndef MATHOPS_H
#define MATHOPS_H
int gcd(int a, int b);
int lcm(int a, int b);
#endif

/* mathops.c */
#include "mathops.h"
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

/* main.c */
#include <stdio.h>
#include "mathops.h"
int main(void) {
    printf("gcd(48, 18) = %d\n", gcd(48, 18));
    printf("lcm(4, 6) = %d\n", lcm(4, 6));
    return 0;
}

How It Works

  1. main.c knows only the declarations in mathops.h.
  2. The linker connects main.o and mathops.o.
  3. The guard keeps the header safe to include repeatedly.

Edge Cases & Pitfalls

Check these before relying on Modular Programming in real programs.

globals

Sharing global variables

Prefer function interfaces over shared globals; they hide implementation details.

def

Defining functions in headers

Non-inline definitions in headers cause duplicate-symbol errors. Put them in the .c file.

Key Takeaways

  • Interface: header.
  • Implementation: source.
  • Private: use static.

One line: Modular programming divides code into modules.

Frequently Asked Questions

It improves readability, enables separate compilation, and lets teams work on different modules independently.
It is one source file after preprocessing, including everything it #includes. The compiler turns each into an object file.
Continue with "C Header Files" next. What belongs in a header and what does not. The C Advanced Concepts hub lists every topic in order.

Next: Header Files

What belongs in a header and what does not.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful