Developing C programs on Mac OS

To develop C programs, there are two things that we need:

  1. First, we need a code editor - this is the program that we will use to write our source code (a source code file is simply a text file, which has a ".c" extension, and which contains valid C code).
  2. Secondly, we need a C compiler - this is a program that converts the source code we have written into an executable file that we can run.

This document outlines the process of installing Apple's command line developer tools (which provide a C compiler) and Microsoft's Visual Studio Code (which is an excellent and free code editor). If you would rather use a different environment, Apple's Xcode is another good option.


Installing the command line tools

The first thing to do is to check if you already have the command line tools installed. To check if you have them, start by opening a Terminal window. You can find the Terminal application from inside the "Utilities" folder within the "Applications" folder. Or, simply search for "Terminal" as shown below:

The Terminal window provides you with a command line prompt - you can type commands at the prompt to carry out tasks. The screenshot below shows a newly opened Terminal window:

Enter the command "gcc" at the prompt. If you have the command line tools installed already, you will see some text output in the window. If you do not have the command line tools installed already, you will see a dialog boxing asking you to install them. An example of this is shown below:

If you need to install the command line tools, click the "Install" button. You will need to "Agree" the license agreement terms as shown below:

It will take a little time to download and install the tools... just be patient!

Once the tools are installed, you will be shown a confirmation message.

The name of the C compiler (that was installed along with the command line tools) is gcc. To check that this is now successfully installed, enter "gcc --version" at the prompt. You should see a few lines of output, showing the version information, as shown below:

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 "hello.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

OK, let's go ahead and compile our first very simple C program using the gcc compiler. We will use a basic text editor to write the source code, and the commands for compiling and running the program are illustrated below:


Installing Visual Studio Code

Now that we have installed the command line tools, let's install a powerful (and free) code editor called Visual Studio Code. We will use this editor to write the source code for our programs. In addition, Visual Studio Code has an "Integrated Terminal" feature which means we can compile and run our program without leaving the Visual Studio Code environment.

To start with, visit the Visual Studio Code website as shown below and click on the "Download for Mac" button:

The download should begin automatically:

Once the download is complete, you can access the "Visual Studio Code" application from the Downloads folder:

It might be a good idea to move Visual Studio Code from the Downloads folder to the Applications folder so that it is more convenient to access in the future:

Next, launch Visual Studio Code and you should see the Welcome page:

Now, install the "C/C++ extension" by click on the "Tools and languages" link under the "Customize" menu:

Choose the "C/C++ for Visual Studio Code" extension and click "Install". If you can't see the list of extensions as shown below, make sure the search box ends in "category:languages c":

Now, let's create a C program using Visual Studio Code. In this example, we have a folder called MyFirstProgram on the desktop, as shown below. We will create a new C source file (in the Visual Studio Code editor) and save it in this folder.

To create a new source file, select "New File" from the "File" menu:

Type the source code for a simple C program into the new file. In this example, the program will just print a short message. Then, save the file as "hello.c" into the MyFirstProgram folder:

In the screenshot below, you can see that the "hello.c" file is saved in the "MyFirstProgram" folder, and the Visual Studio Code editor is showing the contents of the source file.

In order to run the program, we first need to compile it. We will use the "Integrated Terminal" feature of Visual Studio Code (NOTE: this might simply be called "Terminal" in your version of Visual Studio Code). Select this from the "View" menu.

The integrated terminal pane will appear inside the Visual Studio Code environment. In the screenshot below, the terminal pane appears underneath the source code. You can type the same commands in this integrated terminal as you can type from the regular Terminal window - as shown, the source file has been compiled and executed. The output appears directly in the terminal pane.

To make changes to the program, you can edit the source code, save the changes, and compile and run the source code again.

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