« Learn C with Project Builder

Posted by Brad Miller on November 16, 2002 [Feedback (30) & TrackBack (0)]

// Introduction

So, you've decided to learn how to program and want to use your Mac with all the great tools that you've heard comes with it. The only problem is that when you first launch Project Builder you're overwhelmed with all the options and how to set up the tutorial projects that are in your learning C book. Fear not. It's easier than it looks and once you know the basics, it's all downhill.

The goal of this tutorial is to get someone that is new to programming up to speed with how to create, edit, build and debugging a basic project in OS X. Here's how to do it.


// Create the Project

After launching project builder, the first thing you'll need to do is create a new project. You will create one by choosing "New Project..." from the File menu.

This will give you a window with a long list of project types. To create a project for the type of examples that are given in a learning c type of book, you'll want to select the "Standard Tool" option (C++ Tool if learning C++) as shown in figure 1. In Project Builder, a tool is an application that does not have a GUI. In general, items like servers or unix tools that are run using a command line interface are tools. Learning-To-Program books also usually use them since they're teaching you how to program in the language, not how to create a GUI.

Select Standard Tool
Figure 1: Select the Standard Tool option.

Once you have selected the Standard Tool, click the "Next" button. You will then be presented with the window in which you will name the project and set the location at which you'd like to create the project. For this tutorial I named the project learnC as shown in Figure 2. By default, the Location field will create the new project's directory in your home directory. To save in another location, enter the path or use the "Choose" button to select the location.

Finish creating the project by clicking on the "Finish" button. The project will then open up for you.

Naming the Project
Figure 2: Name the Project.


// Working in the Project

The first item of interest is the Project window that is shown in Figure 3. Depending on the options you selected in Project Builder's preferences or when you first launched Project Builder, it'll either be its own window or the left side of a larger window. It shows you various aspects of the project. The item that we're concerned with is the Files tab . It lists out all the files that are included in your project, i.e. source code, resources, images, etc. New groups and files can be added by using their respective menu items under the Project menu.

PB's File List
Figure 3: Project Builder's list of files.

Inside the Source group, you will find the file main.c. It is the file that PB includes to hold your main function. Clicking on it opens up the contents of the file in an editor. By default, it contains a simple Hello World app. You will replace it with the code you're learning about. For this tutorial, we'll use the following code for our application. Replace the Hello World's main function with it. It's a little more interesting the Hello World, but not by much. =)

#include <stdio.h>

int main (int argc, const char * argv[])
  int i;

  for (i = 0; i < 10; i++)
    printf("I can count to %i\n", i);
  return 0;


// Building Blocks

Once you've entered the code that you're writing and have saved the changes, it's time to build the project. Click the hammer icon in the toolbar. Doing so will start the compiler and build the application. It will also cause the build window to appear. The build window shows you any warnings or errors that are generated when compiling your code. If you have any errors, the build will fail and you will need to correct them and then rebuild. If warnings are given, the project will still compile. But before running the program, you should go through the warnings and correct any that you can. While not fatal to the building process, they will usually cause the program to act in unexpected ways or crash.

PB's Build Window
Figure 4: Project Builder's build window.


// Run the Bugs

Now that all the errors and warnings have been taken care of, it's time to run the code. If your program's only user interaction is writing output to the screen like learnC does, the easiest way to run it is to click the "Run" button in the toolbar. It will open a console window that shows everything that you print to the screen. You can also use the console window in PB if the program takes input, but it doesn't show what you print to screen correctly until all of the input is received. The better way to run the program is to use the Terminal. Launch Terminal.app and type ./projectname/build/projectname if you created the project in your home directory. Doing so will run the program in Terminal and allow you to interact with it.

You can also run the program in the debugger. Doing so is can be very useful for finding problems in your code or for seeing exactly what the code in one of the examples is doing. Before invoking the debugger, you have to first set breakpoints to tell the debugger where you want to examine the code. Breakpoints are set by clicking in the margin on the left hand side of the editor at the line you want to stop at. Clicking on a breakpoint a second time makes it inactive and greys it out. To remove a breakpoint, you drag the point out of the margin.

Setting breakpoints
Figure 5: Setting breakpoints.

Once you have your breakpoints set, click the "Debug" button to launch the debugger and your program. You can then step through the code and view what the value of all the variables are as you do so. Play around with the debugger some so that you become familiar with it and know how to use it. Investing a little time now with it can save you a lot of time and headaches when writing something on your own.

Debug window
Figure 5: The Debugger Window.

For the curious, Project Builder uses gcc for its compiler and gdb for the debugger. They are the same tools that are used on Linux and other unixes. This can be helpful because they are often used in education and you will know that your code will compile correctly when you turn it in for a class.


// Conclusion

Those are the basics to get you up and running. You should be all set now to work through your book and become a C master. Just remember to cut me in when you make billions from your killer app. =)


Could you maybe explain what the different type of stepping through the code do.

Posted by: rather not on January 2, 2003 12:48 AM

It doesn´t work for me, I have try, once a copy you code and put it into main.c but it didn´t work.
And if I only take Tool- Standard Tool and don´t change the code in main.c, It says "Build failed (see build log for details)" But I found no log :-(

Posted by: Simon on January 2, 2003 12:21 PM

What I would like to add is a list of free books / tutorials on the net that you as a reader of this tutorial can go on with before you start with Cocoa. On the link :
You can find many usefull C / C++ tutorials where you can learn to program C. Good luck!

Posted by: Mika Sjöman on January 9, 2003 06:35 AM

I would like to add two links to this aswell as the one I added before:

7 day course on C:

Mastering C:

These are a great start!

Posted by: Mika Sjöman on January 9, 2003 06:38 AM

This has to be the greatest free guide:

It's a free C guide at University of Leicester. If you want to start with C - THIS IS THE PLACE! Don't buy any book before reading this.

Posted by: Horace on January 13, 2003 10:49 AM

http://www.eskimo.com/~scs has excellent resources for learning C. Not only does he have class notes for a C course he taught with the K&R book, but he also has a stand along text with which to learn C. Highly reccomended!

Also, I've found the book C Primer Plus by Stephen Prata to be very good - it assumes NO knowledge of computer science.

Posted by: Luke on February 17, 2003 10:33 PM

how do you know what syntax is used in the C++ tool.
i try entering

cout << " it's not working";

Posted by: Ron on March 16, 2003 02:32 AM

while i am certainly no fan of c++, your code snippet appears to be missing some meat. you might try something similar to:

using namespace std;

main(int argc, char *argv[])
cout << "monkey." << endl;

Posted by: jean-pierre on March 17, 2003 11:36 AM

The correct version of this program is:

using namespace std;

int main(void)
cout << "monkey." << endl;
return 0;

Posted by: Ben on April 16, 2003 01:35 PM

Correction: change "#include" to "#include ", without the quotes, of course.

Posted by: Ben on April 16, 2003 01:36 PM

hey man, just want to say hi

Posted by: ip address on May 4, 2003 03:26 AM

Thankyou so much! Now I might actually have a chance of passing first year Computer programming because I have somewhere to practice at home!

Mathieu T. Australia

Posted by: Mathieu Tozer on May 13, 2003 10:34 AM

im need the learn of mmb.
thank you

Posted by: hadi on May 29, 2003 08:12 AM

All this time I have been using Borland C++ for my course on C++ programming. I would like to THANK YOU for posting this tutorial, because now I can use my ibook instead of my crummy PC.

Question though: If my instructor requests to view the source file - can they view it on a PC program such as codewarrior or Borland?

Posted by: Rose on June 4, 2003 04:42 PM

Yes, it shouldn't be a problem. The source files are just plain text files. Just give him the .h and .cpp files, not the project file. It won't be of use to him.

Posted by: Brad on June 4, 2003 10:24 PM

First thanks for the tutorial! I was able to run the simple build in Terminal. I was wondering about 2 things. I understand gcc is a compiler. I tried to compile the same main.c file through Terminal. I typed gcc main.c. It produced a file called 'a.out' What do I need to do with that file in order to run that program? And how do I use gdb directly in the Terminal. basically I want to be able to compile these basic Standard Tools in Terminal without using Project Builder. Is that possible?

My ultimate goal for now is to be able to write a plug-in for 2 Adobe apps. Those SDK's are based on Code Warrior 7, needless to say I don't have the money for CW7 and I don't have the money to become a Premium Level Member of (ASN) Adobe Solutions Network.

-- ANYONE, please email me if you can contribute.

Posted by: Perpetual Newbie on June 10, 2003 05:33 PM

Cool stuff. Helped me bridge between using vi for writing the basics of C to using PB.

Now....where to go next. Hello world here I come!

Posted by: Matt on June 14, 2003 07:21 AM

just to cover perpetual newbie's question about gcc

a.out is the default name for a program when compiled in gcc
to run it, just use


in the terminal, and it will execute the a.out file as a program.


Can I just say this site is very useful - found some very nice things that I plan to use for a small project, but I wish there was more java coverage. There seems to be a very strong objective C emphasis and it could do with a few examples of how to do the same things in java...

Just constructive criticism. Anyway, keep up the good work...

Posted by: Andrew Howat on June 23, 2003 11:26 AM

is the new link referring to Horace's post on the great free C guide at the University of Leicester.

Posted by: Lenny on June 24, 2003 03:12 PM

Hi -
I am trying to do the very thing this tutorial is meant to teach - which is use project builder to implement examples in a learning C book. I don't want to create a new project for each example and was wondering how you can have multiple executables in the same project - clearly there can be only one main.c file, but if you have several files with main functions, is it possible to build several executables within the same project?

Posted by: Amit Gandhi on June 28, 2003 02:03 PM

Yes, just make a new target from the project menu. Then add and check on/off the files that you want to be included in that executable.

Posted by: Brad on June 29, 2003 01:36 AM

This is very helpful, but,... When I debug the code below, it runs fine - the first printf then the scanf and then the loop. But, when I run in w/o the debug (all in PB) it sits until I enter a number and then does the first printf and then the loop.

Also, when I run the app in the command line it works fine.

Any ideas - is this a PB bug?


int main (int argc, const char * argv[])
int i;
int max_count;

printf("Enter a number to count to: ");

for (i = 0; i < max_count; i++)
printf("I can count to %i\n", i);

return 0;

Posted by: David on July 11, 2003 03:08 PM

I have two questions. First, is there any way to make project builder launch the app in terminal? And second, for classes that require make files does project builder create these files? Thanks.

Posted by: Michael on July 13, 2003 07:03 PM

Your are not the only one.

Posted by: postal code on July 24, 2003 09:49 AM

I have problems with printf and scanf alignment. A problem with the correct sequence of readings and writings when run from the console, does not work properly when run from Project Builder. In particular, if the program does a printf and then a scanf the ouput first waits for the scanf and then shows the result of the printf. It is not a big deal, but it bothers me. Some hint?


Posted by: Claudio Sartori on July 31, 2003 10:02 AM

Your problem is the I/O buffering. Just place a "fflush(stdout);" (without the quotes) between your printf() and scanf() statements.

Posted by: Sascha on October 12, 2003 12:36 PM

Hello. I just wanted to give a quick greeting and tell you I enjoyed reading your material.

Posted by: Atkins Diet on November 9, 2003 02:28 AM

HI, Wow.. this is a very informative website!
I enjoy your site very much! Keep up the good work!
Very nice design!:)

Posted by: hgh on November 14, 2003 01:46 PM

Nice comment.

Posted by: atkins diet on November 17, 2003 01:22 AM

thanks to the first several people for the links.

Posted by: Micah on November 17, 2003 06:07 AM
Post a comment