Open In App

Multiple Source Files in C

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using as a header file
  2. 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

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



Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads