Last Updated : 11 Jul, 2025
Modular programming is the process of subdividing a computer program into separate sub-programs. A module is a separate software component. It can often be used in a variety of applications and functions with other components of the system.
Example of Modular Programming in C
C is called a structured programming language because to solve a large problem, C programming language divides the problem into smaller modules called functions or procedures each of which handles a particular responsibility. The program which solves the entire problem is a collection of such functions. Module is basically a set of interrelated files that share their implementation details but hide it from the outside world. How can we implement modular programming in c? Each function defined in C by default is globally accessible. This can be done by including the header file in which implementation of the function is defined. Suppose, we want to declare a
Stackdata type and at the same time want to hide the implementation, including its data structure, from users. We can do this by first defining a public file called
stack.hwhich contains generic data Stack data type and the functions which are supported by the stack data type. In the
header filewe must include only the definitions of constants, structures, variables and functions with the name of the module, that makes easy to identify source of definition in a larger program with many modules. Keywords
externand
statichelp in the implementation of modularity in C.
stack.h: extern stack_var1; extern int stack_do_something(void);
Now we can create a file named
stack.cthat contains implementation of stack data type:
stack.c #include int stack_var1; static int stack_var2; int stack_do_something(void) { stack_var1 = 2; stack_var2 = 5; }
The main file which may includes module stack
#include int main(int argc, char*argv[]){ while(1){ stack_do_something(); } }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4