Multiple Source Files in C
Last Updated :
11 Jan, 2023
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
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
#include "second.c"
#include <stdio.h>
int main()
{
int a = 4, b = 5;
int ans = sum(a, b);
printf ( "Sum: %d" , ans);
ans = sub(a, b);
printf ( "Subtraction: %d" , ans);
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
int add( int a, int b)
{
return a + b;
}
|
Second Program:
C
#include <stdio.h>
int add( int a, int b);
int main()
{
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
Share your thoughts in the comments
Please Login to comment...