C Code Organisation
C allows us to split code up over multiple files so we can organise our applications and keep related functions in the same place.
Code and Header Files
When we create functions in C we usually have a definition and a declaration as shown below.
int max(int i1, int i2); // function definition
int max(int i1, int i2) // function declaration
{
if (i1>i2)
return i1;
else
return i2;
}
The parameter names in the definition aren’t required, we just need to define the types.
int max(int, int); # function definition
In a single file application we define functions near the top of the file, this way we don’t have to worry about ordering each function so it is declared before it is. Typically it is easier to read code that starts with the main()
function and works its way down the function calls, defining our functions ahead of time makes this easier.
Eventually we will get to the point where our file is cluttered with code and difficult to maintain. By splitting it in to multiple files with each file holding relating functions it is easier to understand and manage.
C achieves this using code and header files.
Code files hold the function declarations.
// my_functions.c
int max(int i1, int i2) // function declaration
{
if (i1>i2)
return i1;
else
return i2;
}
int mix(int i1, int i2) // function declaration
{
if (i1<i2)
return i1;
else
return i2;
}
int different(int i1, int i2) // function declaration
{
if (i1>i2)
return i1-i2;
else
return i2-i1;
}
Header files hold the function definitions
// my_functions.h
int max(int, int); # function definition
int min(int, int); # function definition
int difference(int, int); # function definition
Each file has the same name but a different extension - .c for the code files and .h for the header files.
You can use the functions in any other c file by using #include
to include the header file. In this case you’d use #include "my_functions.h"
. Note that you use “” to include youir own header files, rather than <> to library files (such as ‘#include
// main.c
#include "my_functions.h"
main()
{
printf("%d",max(3,4));
}
C & Multiple Definitions
Just as with Java, C doesn’t let you define the same function more than once. This can cause problems with multi-file projects. If my project has a main.c
and a simulation.c
and I want to use my_functions in both I need to add the line #include "my_functions.h"
to both files, but this means I am defining the same functions twice and C won’t let me do that. To resolve this we use a “guard” structure that prevents the definitions being repeated.
// my_functions.h
#ifndef _MY_FUNCTIONS_H
#define _MY_FUNCTIONS_H
int max(int, int); # function definition
int min(int, int); # function definition
int difference(int, int); # function definition
#endif /* _MY_FUNCTIONS_H */
This #ifndef, #define, #endif
structure prevents the code from being processed more than once. Simply put it says
if (_MY_FUNCTIONS_H doesn't exist)
{
create _MY_FUNCTIONS_H // think of this as a flag
// do the rest of the header
}
Global Variables
If we create any global variables in our files then we need to add defintions for them in our header file.
// my_functions.c
int global_integer;
int max(int i1, int i2) // function declaration
{
if (i1>i2)
return i1;
else
return i2;
}
// my_functions.h
#ifndef _MY_FUNCTIONS_H
#define _MY_FUNCTIONS_H
extern int global_integer;
int max(int, int); # function definition
int min(int, int); # function definition
int difference(int, int); # function definition
#endif /* _MY_FUNCTIONS_H */
Struct Definitions
Any structs
we define should be placed in the header rather than the code file. (Included for completeness, we will return to this later).
- Previous
- Next