Multiple Source Files in C
Last Updated :
28 Apr, 2025
C programming language, as it is a procedural programming language sometime we need two or more programs linked together to store all the methods together. So, let's check how to link two C source files. Methods to compile multi-file c program are given below:
Methods to use two or more source code files in C
There are two methods to use two or more source code files in C as mentioned below:
- Using as a header file
- Using --gcc commands
1. Using as a Header file
After creating two separate files we can use one of them inside another file by using another file as a header file. Header files are used to use the properties from the already written files.
Example:
#include<stdio.h>
// Here we are loading stdio file containing data about printf,scanf,etc.
So, how we can use another file let's check with an example program.
First Program:
C
// C Program second.c
// to be used in first.c
int sum(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
Second Program:
C
// C Program to use code another
// file into this file
#include "second.c"
#include <stdio.h>
int main()
{
// declared two variables
int a = 4, b = 5;
// sum function called
int ans = sum(a, b);
printf("Sum: %d", ans);
// sub function called
ans = sub(a, b);
printf("Subtraction: %d", ans);
// multiply function called
ans = multiply(a, b);
printf("Multiply: %d", ans);
return 0;
}
Output:
The output of the first.c
In the above program, we were able to use the functions declared in second.c by just including the program using #include "second.c".
2. Using GCC commands binds both programs together
In this method, we will use the gcc command for running the program because of which we bind both the programs together while running. But, before running the program let's check how C programs work.
How do C Programs work?
Firstly let's get an idea of how a C program build. It goes from the following steps :
- Preprocessing
- Compiling
- Linking
In preprocessing only comments are removed from the source code and header files code is pasted in the file also macros are placed where they are used. After this, In compiling texts are converted to machine code but function calls are not mapped with function definitions. In linking the mapping between function calls and function definitions is happened as its name suggests 'linking'. It links the function calls and function definitions.
At the linking phase, we have to add all the compiled C files (object files) so that the linker links all the files and executables can be formed.
Command to build a C file with more than one C source code file
gcc -o main firstFile.c secondFile.c
Example
First Program:
C
// secondFile.c
// C Program to implement
// using gcc commands
int add(int a, int b) // basic add function
{
return a + b;
}
Second Program:
C
// firstFile.c
// C Program to implement
// Using gcc commands
#include <stdio.h>
// just declaring the function
int add(int a, int b);
int main()
{
// In this there is no add function definition. add
// function definition is in another file.
printf("%d\n", add(14, 16));
printf("%d\n", add(18, 16));
return 0;
}
Demonstration:
Demonstration of the given example, code result of the above codes
See the commands in the below image for a demonstration.
Command and result of the above example
Similar Reads
Build a C++ Program that Have Multiple Source Code Files In C++ generally, we saw only a single file program but what if we have multiple source code files and we have to build a program by using all these files? Let's see how we can build a C++ program with multiple source files without including them as header files. Use two or more source code files in
3 min read
C File Pointer A file pointer is a variable that is used to refer to an opened file in a C program. The file pointer is actually a structure that stores the file data such as the file name, its location, mode, and the current position in the file. It is used in almost all the file operations in C such as opening,
3 min read
fsetpos() (Set File Position) in C The fsetpos() function moves the file position indicator of the given file stream to the specified position. When fsetpos() is executed, the end-of-file indicator is reset. fsetpos() function is a part of <cstdio> header file in C Standard Library. Syntax of fsetpos()int fsetpos(FILE *stream,
2 min read
C - File I/O In this article, we will learn how to operate over files using a C program. A single C file can read, write, move, and create files in our computer easily using a few functions and elements included in the C File I/O system. We can easily manipulate data in a file regardless of whether the file is a
11 min read
How to Concatenate Multiple Strings in C? In C, concatenating strings means joining two or more strings end-to-end to form a new string. In this article, we will learn how to concatenate multiple strings in C. Example: Input:char str1[50] = "Hello";char str2[50] = " geeksforgeeks";Output:Hello geeksforgeeks!Concatenating Strings in CTo conc
1 min read