Splitting a Program into Multiple Files

Learn about a general practice to divide code into different categories or files.

The programs we have seen so far have all been stored in a single source file. As our programs become larger and as we start to deal with other people’s code (e.g. other C libraries), we have to deal with code that resides in multiple files. Indeed, we may build up our own library of C functions and data structures that can be reused in our own scientific programming and data analysis.

Here, we’ll see how to place C functions and data structures in their own file(s) and how to incorporate them into a new program.

Reusing code

We saw in the section on functions that one way of including custom-written functions in our C code is to simply place them in our main source file—either above the declaration of the main function or below it with the function prototype appearing above the main function.

A better way to reuse functions that we commonly incorporate into our C programs is to place them in a separate file, and to include a directive above main to include that file.

When compiled, it’s just like the code getting copy pasted above the main function, but for the purpose of editing and writing our code, this allows us to keep things in separate files. It also means that if we ever decide to change one of those reusable functions (for example, if we find and fix an error), then we only have to change it in one place and don’t have to go searching through all of our programs and change each one.

Header files

A common convention in C programs is to write a header file (with .h suffix) for each source file (.c suffix) that we link to you main source code. The logic is that the .c source file contains all of the code and the header file contains the function prototypes, that is, just a declaration of which functions can be found in the source file.

This is done for libraries that are provided by others, sometimes only as compiled binary “blobs” (i.e., we can’t look at the source code). Pairing them with plain-text header files allows us to see what functions are defined, and what arguments they take and what type of values they return. In addition to function prototypes, header files can also contain other things like macros, type definitions and variable declarations using const.

The #include directive

The #include directive is used to link a header file or a library with a source file. We’ve already seen a very prominent example of this: #include <stdio.h>. This indicates that we want to include all the content of stdio.h into our source file.

So, for any source file, the template for using the #include directive is:

  • #include <file name> or
  • #include "file name"

An example

Here is a program that computes the “preferred direction” of a neuron recorded in primary motor cortex of rhesus macaques, during a whole-arm reaching task (e.g. from Gribble & Scott, 2002). The monkey moved his hand from a central start target to one of 8 peripheral targets around the circumference of a circle. Movements to each of the 8 targets were repeated 5 times for a total of 40 movements.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy