Separating Your Program Into Multiple Files
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
By now your first program is slowly growing in size, you’ve created a couple of functions and hopefully they work. Your program still doesn’t do all that much. That’s ok, you need to learn to walk before you can run, and right now we’re still crawling. I promised I would talk about separating your program into multiple files and why that’s useful. Before we dive into Xcode and explore .c and .h files lets talk about why we would want to split our program up into little pieces.
Some Theory
If you think about our foray into functions we’ve already begun the process of logically separating our code. Functions allowed us to break up repetitive tasks so that they can be re-used, separating our code into multiple files follows a similar pattern of thought.
If you wanted to use your looping functions in another program you would have to copy and paste them from one project to another. Chances are after a while you would end up with a lot of the same code written in different programs. If, one day, you found out a better way to write those looping functions you would then have to edit every application. What a pain!
You’re already using one function that has been separated out for your convenience. The printf function is part of the standard input-output header file (stdio.h) of the C language. Imagine if you had to write the code for these basic functions yourself every time you wanted to use them in a program!
The Implementation
Lets get started splitting out your functions into their own file for easy maintenance. Fire up Xcode and open the MyFirst project. First we need to create a new file, select File - New File from the Xcode menu. THe file we want is a C File located under the BSD heading.
Name your file loops.c and ensure that the Also create “loops.h” is checked. You should be able to leave everything else as is.
Xcode should create two new files for you, loops.c and loops.h. The first is where the code for our functions will be placed, the second is where we will declare our functions. Before we do anything though, you should move the two new files into the source folder of your Xcode project. It’s good practice to keep your source code structured so that you can easily find things later. A simple drag and drop will move them for you.
It’s time to shift some code around. loops.c should contain the implementation of your functions. Go ahead and copy the forLoops function out of main.c and paste it into loops.c. Including the comments at the top you should end up with something like this:
/*
* loops.c
* MyFirst
*
* Created by Gareth Townsend on 15/02/07.
* Copyright 2007 http://www.colourmecocoa.com/ All rights reserved.
*
*/
#include "loops.h"
int forLoops(int repeat) {
int i = 0;
for(i = 0; i < repeat; i++) {
printf("This is a for loop within the forLoops function\n");
}
return 0;
}
You’ll notice that we’ve included loops.h, the two files go hand in hand. loops.h is a header file, it contains the definition of our functions, it’s like a table of contents for loops.c. Because of this we need to define forLoops in loops.h before we can use our two new files in main.c.
/*
* loops.h
* MyFirst
*
* Created by Gareth Townsend on 15/02/07.
* Copyright 2007 http://www.colourmecocoa.com/ All rights reserved.
*
*/
#include <stdio.h>
int forLoops(int repeat);
The definition is very similar to the first line of our implementation. The only difference is the use of a semi-colon to end the line instead of a pair of curly brackets to contain the implementation. Note that I’ve also included stdio.h, without it loops.c will not know about the printf function. main.c is now very small.
#include "loops.h"
int main (int argc, const char * argv[]) {
// insert code here...
forLoops(5);
return 0;
}
I did notice that while moving the header file declarations around Xcode didn’t complain if loops.h was no included. My guess is that Xcode is looking in the source directory for all files when compiling the program. It’s good coding habit to include the header files you are using, so I’ve left it in the code.
Separating your program into multiple files is a snap! Your mission, should you choose to accept it: Continue moving the other looping functions into the loops.c and loops.h files. You did create them last time didn’t you?


