Developing C programs on Mac OS (using Xcode)

A newer version of this document describes how to use Apple's command line tools along with Microsoft's Visual Studio Code to develop C programs on a Mac. You should consider this newer approach before following the instructions on this page. However, if you would prefer to install Apple's Xcode environment... read on! This document outlines the process of installing Xcode on Mac OS, which provides a compiler for C source code. Two approaches are described:

  1. the first illustrates how to write, compile and execute C programs in the Mac OS Terminal window using the command prompt tools
  2. the second illustrates how to use the graphical Xcode environment to build and run C programs


Installing Xcode

To begin, you need to install Xcode. Start by launching the App Store:

If you have trouble finding Xcode, you can search for it in the App Store:

Xcode is available for free. Depending on the version of Xcode and your version of OS X, this installation may automatically include the command line tools which we need. Certainly, if you are running anything later than OS X 10.9, Xcode comes bundled with all of the command-line tools.

If you find the command line tools are not included automatically (for example, you are not running OS X 10.9 or later), you can always install them manually by launching Xcode, and then, from the Xcode menu, selecting "Preferences" and "Downloads" and installing them from the dialog box:


Two options

Great, now that Xcode is installed, you have two options for developing and running C programs on your Mac. The first option involves using Xcode as an editor only to write your source code, and using the "gcc" command within the Terminal window to compile your code. The second option involves using Xcode as the development environment, and creating an Xcode "project" to organise your files.

Both options are described next - use whichever you prefer...


Option 1: Using Xcode as an editor and the Terminal window to compile

Editing the source code

In this example, I have created a new folder called "C" in the "Documents" folder. Notice that at this stage, there is nothing in this folder:

OK, let's create a source file and store it in this "C" folder. We are going to launch Xcode, but use it as an editor only to write our code. Begin by launching Xcode:

Instead of selecting either of the prompts on the main window, such as "Create a new Xcode project", simply select "New > File..." from the File menu:

Choose the "C File" type from the dialog box as shown below:

And then select the location where you would like to store the C source file - in this example we are going to store our new file inside the "C" folder which is in the "Documents" folder:

Finally, give the new source file a name. The most important thing to remember here is that the extension for this file (i.e. the suffix at the end) must be ".c". So, in this example, we have called the source file "example.c":

You will now see an (almost) empty template for your C source file. It is useful to also keep in mind where on disk this source file is stored. The screenshot below shows the contents of the C source file as well as the file stored on disk in the "C" folder:

OK - now go ahead and write the source code for your program directly in the editor. In this example, I have created a simple "hello world" style program, which should just print out a welcome message:

Compiling and running your program

Excellent - now that you have written the source for your program, you must compile this source file before you can run the program and observe the output. To compile the source, open a "Terminal" window in Mac OS. You can find the Terminal from in "Applications/Utilities" or simply search for "Terminal":

From the Terminal window, you can view which directory is current by typing:

You can change directories by typing:

You can list all of the files in the current directory by typing:

And finally, you can compile your source file with the command:

In this case, "runme" is the name of the executable file that will be created if your code compiles successfully, and "example.c" is the name of the source file. In the screenshot below, you can see that a new file called "runme" was created in the same directory as the source file.

To run the executable, you must type:

which means: "execute the program named "runme" which is the current directory

These commands are illustrated below:

And that's it - you can now edit the source file, re-compile the code, and run the program again!

Good luck!


Option 2: Using Xcode as your development environment

In this example, we are going to use Xcode as our development environment - we will write our source code, and compile and execute our program without leaving the environment or using the Terminal window as we did in the previous example.

We are going to create an Xcode "project" and store this in a folder called "C" in the "Documents" folder. Notice that at this stage, there is nothing in this folder:

Begin by launching Xcode, and notice that we are given the option to "Create a new Xcode project":

Select "Create a new Xcode project", and then choose "Command Line Tool" from under the OS X category for the project template:

Choose a name for your project - in this example I will use "ExampleProject":

We are then prompted to select where we would like to store the project and it's associated files. In this example I will store the project in a new folder I created called "C" which is inside the "Documents" folder:

It is useful to take a look at the new files that Xcode created when setting up this new project called "ExampleProject". You can see, by looking on disk, that a new folder called "ExampleProject" was created inside the "C" folder, and inside this folder are two other folders. Finally, inside the second "ExampleProject" folder is our actual source file - this is called "main.c":

Xcode will automatically show you lots of information about the project:

But the most important thing as far as we are concerned is the source file called "main.c". You can find this in the top left hand corner, under the "ExampleProject" heading:

Double click "main.c" to look at the source code. A template is already provided, but take a look at the existing main() function definition... we will change this to "int main(void)":

We will now write the source code for our simple "hello world" program:

When we are finished writing our source, we can click the button in the top left hand corner that looks like an arrow. This will compile and (if there are no syntax errors) execute our program:

After clicking the button, we will see the output of the program in the bottom right hand pane:

To change the program, we just modify the source code, and repeat the process. We can even write programs that obtain input from the user - you can type this input directly into the Xcode output pane:

That's all there is to it! Good luck!