← Back to C Course | Chapter 14: Additional Topics | Lesson 3 of 9

C Multi-file Programming

Multi-file programming splits a big program into several files, like chapters of a book. They are compiled separately and joined together at the end.
Syntax
c
// file.c
return_type function_name(parameter_type parameter) { /* definition */ }

// main.c
#include "file.h"
extern type variable_name;   // defined in another file

// gcc main.c file.c -o program

Modularizing Code

Splitting a growing program across multiple source files keeps each file focused on one area of responsibility, making the overall codebase easier to navigate, easier to test in isolation, and easier for more than one person to work on at the same time.

Example: Modularizing Code

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

The extern Keyword

The extern keyword tells the compiler that a variable is defined in a different source file and should be linked to that existing definition rather than treated as a brand-new variable, which is what allows a global value to be shared safely across multiple .c files.

Example: The extern Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare sharedCount and set it to 10
int sharedCount = 10;
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", sharedCount);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Compiling Multiple Files

Building a multi-file C program means passing every relevant .c file to the compiler in a single command, such as gcc main.c tools.c -o program, so the compiler can compile each file separately and the linker can then stitch their object code together into one executable.

Example: Compiling Multiple Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Multiple .c files compiled into one program");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Sharing Function Prototypes

Putting a function's prototype in a shared header file, then including that header both in the file that defines the function and in every file that calls it, lets the compiler verify each call matches the function's real signature without needing to see the function's actual implementation.

Example: Sharing Function Prototypes

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Benefits of Multi-file Projects

Beyond just organizing code, splitting a project into multiple files lets separate contributors edit different files at the same time with far less risk of merge conflicts, and lets a build system recompile only the files that actually changed rather than the entire project every time.

Example: Benefits of Multi-file Projects

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
int cube(int n) { return n * n * n; }
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", square(3), cube(3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting extern when sharing a global variable, so each file gets its own copy or the linker fails.
  2. Defining the same function in two files, causing a duplicate symbol error.
  3. Forgetting to list every .c file in the compile command.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.