CodeBlanks Logo Icon CodeBlanks

Summary - Multi-file

  • C course
  • Intermediate
  • Lesson 64/116
  • 5 min

Combine header, extern and Makefile to write a two-module project.

Summary - Multi-file

In this group you learned to split C projects into modules: header/source separation, include guards, sharing with extern, hiding with static, build automation with Makefile, and combining multiple modules. These are the building blocks of real C projects.

Summary task: simulate a two-module calculator in one file. One module holds arithmetic functions (add, mul), the other keeps a counter (static). main calls both. This pattern is a small slice of large projects; it shows the foundation of modular design.

A two-module calculator

The two-module simulation: the first has calc_add and calc_mul functions; the second holds a static counter call_count and a record_call function. record_call increments the counter each call.

main calls calc_add and calc_mul, then calls record_call after each. Finally it prints the counter. This is the skeleton of a real C app: function module + state module + main. A Makefile compiles and links these three files; here we simulate in one file.

Practice Write a two-module calculator

Write int calc_add(int a, int b) { return a + b; } and int calc_mul(int a, int b) { return a * b; }. Write void record_call(void) { static int c = 0; c++; printf("%d\n", c); }. In main add calc_add(2, 3); record_call(); calc_mul(4, 5); record_call();. Don't forget <stdio.h>.

Starter code

main.c
#include <stdio.h>

// Write calc_add, calc_mul and record_call

int main(void) {
    // Call calc_add and calc_mul, then record_call
    return 0;
}

Sample solution

main.c
#include <stdio.h>

int calc_add(int a, int b) {
    return a + b;
}
int calc_mul(int a, int b) {
    return a * b;
}
void record_call(void) {
    static int c = 0;
    c++;
    printf("%d\n", c);
}

int main(void) {
    calc_add(2, 3);
    record_call();
    calc_mul(4, 5);
    record_call();
    return 0;
}

Expected output

1
2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How do you hide a module's internal helper?

  • With static Correct answer
  • With extern
  • With #include
  • With a Makefile

Explanation: static makes it file-local and hides it.

2 Match module tools

Term Meaning
header Public API
static Internal helpers
extern Global sharing
Makefile Build automation

All lessons in Intermediate