Building Your First Program In Xcode
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!
It’s time to write some code. You’ve installed Xcode right? Maybe you’ve even opened it and poked around a bit? Or maybe you opened it up and looked at your desktop wondering what the hell to do? Never fear! Help is on the way.
Cocoa applications are generally written in Objective-C, which is a superset of the C programming language. It adds an understanding of Object-Oriented programming to the C language.
I’m going to say this now, if you’re a gun at C programming then a lot of this is going to be very old hat for you, skim the content and pick out the Xcode specific information. If you’re new to all of this, or you’re a little rusty when it comes to C you should feel at home here. In any case I would apcodeciate any feedback in the comments or via the contact page.
So go ahead and fire up Xcode. Not much is going to happen, you might even be asking yourself if you did something wrong? Click the Xcode icon in your dock and have a look at the menu bar, if you have Xcode up there then you’re doing fine. Go to the file menu and select New Project.

Scroll down through the available options until you find Command Line Utility, then select Standard Tool and click Next. Give your project a name, I’m using myFirst and chose an apropriate directory to store the project files in, the default is your home directory as indicated by the ~ (tilde) symbol. Click Finish and Xcode will open up the main project view.

This window is split into three main areas, the menus that run along the top, the groups and files associated with the project, and a list of files on the right. You should see three files, main.c, myFirst and myFirst.1. The only one we are interested in for now is main.c so go ahead and open it up by double clicking it. You should be codesented with the following code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
return 0;
}
Go ahead and click the Build and Go button. Congratulations you just built and ran your first program. This program doesn’t do much, all it does is print the words Hello, World! to the command line, but it’s a start.
Lets have a look at what each line of code actually does then shall we? The first line of code
#include <stdio.h>
is fairly self explanatory. It tells the compiler (the program which turns your code into a program your mac can understand) to include the file named stdio.h. C programs are split into header files (.h) and actual code files (.c). The header file contains a list of commands, whereas the code file contains the implementation. By including stdio.h in our program we are telling the compiler that we are going to use some of the commands in the Standard Input/Output library.
The second line of code
int main (int argc, const char * argv[]) {
is a bit confusing, and even scary at first. The good news is that you don’t really have to understand it, just make sure it is there. Lets pick it apart anyway. The first two words int main tell us that the main function returns a type int.
Huh?
Most functions return a value, it lets the calling function know what happened. Every program has to have a main function, it’s where every other function is called from. The main function returns an int, or integer, meaning a whole number. If you jump down to the second last line of code you will see the line
return 0;
which is the number (int) being returned by the main function when it completes. 0 indicates that everything went fine and no errors were encountered. Back to the second line of code for now.
(int argc, const char * argv[]) {
Enclosed in the brackets () are two arguments required by the main function. You do not need to know what these are and I am not going to explain how they work, for now you merely need to accept that they are required.
The very last character of this line contains an opening curly brace. This signals the beginning of the main function, everything enclosed within this opening curly brace and the closing curly brace on the final line is the main function.
The main function of myFirst doesn’t contain much, because this program doesn’t do much at all. The main function includes:
// insert code here...
printf("Hello, World!\n");
return 0;
The first line is a comment. A comment starts with // or can be enclosed within /* and */. Anything that is a comment is ignored by the compiler, it’s skipped over. Why would you want to put in comments? So that when you return to your code in a few days/weeks/years you will be able to easily figure out what it was that your code does.
Next we have the printf statement, a function included in stdio.h! This is the part of our program that does all of the work. It contains the string “Hello, World!\n”. This is the string that is printed to the command line. You may be wondering what the \n is for, it’s the code for a new line. If you delete it and run the program again, you will notice the blank line under Hello, World! disappears.
The final part of the line is a semi-colon. Every command requires a semi-colon to indicate to the compiler that the command is finished. The return statement in the line below also has a semi-colon at the end of it for the same reason.
Phew! That’s a lot to digest if you’ve never seen any C code before. I urge you to experiment with the code and leave a comment on this post if you have any troubles or discover something that confuses you. Here are a few things to try out:
- Add more comments, in different places
- Add more printf statements
- Experiment with \n and its placement within strings
- Try adding another return statement, different numbers, positions etc.
Next time we will modify this program to do a bit more than simply print text to the screen.
February 21st, 2007 at 4:49 am
Great explanations for beginners! Thank you, and keep up the good work!
February 21st, 2007 at 12:46 pm
Thanks Andrew. Glad you enjoyed it.
March 29th, 2007 at 3:08 pm
Hi I’m a absolute beginner that wants to learn but all i have found was too difficult for me, i’m glad that i found this site, helps me a lot, thank you very much
March 30th, 2007 at 4:17 pm
Your welcome Carlos. I’m glad you’re able to learn from it.