Splitting your program into multiple files

It is a general practice to divide your code into different categories or files. This section will teach you how to do this correctly.

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

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

We saw in the section on functions, that one way of including custom-written functions in your C code, is to simply place them in your main source file, above the declaration of the main() function.

A better way to re-use functions that you commonly incorporate into your C programs is to place them in their own file, and to include a statement above main() to include that file.

When compiled, it’s just like copying and pasting the code above main(), but for the purpose of editing and writing your code, this allows you to keep things in separate files. It also means that if you ever decide to change one of those re-usable functions (for example if you find and fix an error) that you only have to change it in one place, and you don’t have to go searching through all of your 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 you link to your 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. you can’t look at the source code). Pairing them with plain-text header files allows you see what functions are defined, and what arguments they take (and return).

The #include statement

The #include statement 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>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 statement 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. The order of target directions was randomized.

Create a free account to access the full course.

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