C is for Cocoaby Seth Roby
Editor's Note -- Back in May of this year, Mac DevCenter readers turned out in full force for our first reader survey. One of the requests that jumped out from the survey results was a resounding request for more Cocoa related articles. I have good news for you, this first installment in a series by Seth Roby. I really enjoyed this debut piece, and I hope you do too.
Lesson 1: Stay within the Lines
Have you ever tried learning Cocoa? You get all worked up over your idea: the Next Killer App, and you set out to use what is, by popular opinion, one of the greatest software development platforms there is. You fire up your web browser and Google for a tutorial, and the first paragraph hits you like a brick wall: each tutorial you find assumes you know C. So you dutifully head off and look for a C tutorial, and find that the ones that are comprehensible include three lifetimes of detail. Where is that ease of development you heard about?
According to those other tutorials, to learn Objective-C you need to know C, and to learn C you need to wade through a sea of things that, quite frankly, won't help you at all as you start out in your Cocoa experience. But if that's the case, why do the tutorials require this as base knowledge?
The simple answer is because it's easier to write those tutorials that way. They are there to teach you Objective-C, which is (unlike C++) a strict superset of C: Everything in C is valid in Objective-C, with no changes.
This tutorial is different: here, you will learn all the C you need to know to learn Cocoa, and ignore the rest: these lessons will leave out the parts of C that, while useful, are not necessary to know in everyday Cocoa programming. Whenever possible, the C you learn here will be connected with examples of when you'll use it in Cocoa. It is a jump-start method designed to get your foot in the door and start you off running. This is not to say that it will teach you all the C that you need to use all of Cocoa: eventually, as your skills expand, more and more of C will become useful to you, and you can pick the more advanced bits of C up as you go. What this series teaches you is the amount of C you need in order to start out and make some progress.
A quick aside for those who do know a bit of programming: this tutorial is meant to bring people from a standing start up to speed. As such, the pace may seem slow if you have prior experience, but there are plenty of faster tutorials out there, if this one isn't fast enough for you.
In order to start out at the beginning, we need to start out at the smallest bit of programming: the line of code. Just like a sentence is the basic meaningful unit of speech, a line of code is one statement that carries a meaning: do this thing, make this change. If you know your syntax lingo, every line of code is an imperative: you are always telling the computer what to do.
In C, a line of code looks like this:
int favoriteNumber = (3 * 4) / 2;
This line shows an example of variable assignment. Let's pull it apart and see what all the bits do.
The first thing you'll notice is that it looks a bit like an equation
you might have seen in algebra class back in school. Which is
perfectly correct: this is an equation, and what we're doing is
putting the results of that equation into a variable, which is simply
a place to store something. Our variable is named favoriteNumber, and
after this line of code is executed, favoriteNumber will hold the
line of code could be translated to 'remember that my favorite number
But that doesn't quite tell the whole story. We still have little bits at the beginning and end that don't make sense: what is this 'int', and what's that semicolon doing there?
You might have guessed the semicolon's function already. It's like the period in English: it tells the computer where one line of code ends and the next one begins. Let me say that again: in C, a semicolon ends the line of code, not the carriage return. We could just as easily have written out our code like this:
int favoriteNumber = (3 * 4) / 2;
The extra return in there is completely ignored by the computer. It doesn't matter how many you put in, and the same goes for spaces and tabs. Collectively, these three (spaces, tabs, and returns) are called whitespace, and C ignores how much you have: the only important thing is that there needs to be some whitespace between the different words (int, favoriteNumber, etc), which in C are usually called tokens.
Now, since the semicolon is like a period, ending a line, you might think that int is like a capital letter, beginning it. But it's not that simple (and a good thing, too: you'd have to type 'int' far too often!). Instead, int is a type, and stands for integer. There are many types in C (you can even define your own, but that will come later), but for now we'll focus on integers. An integer, as you may recall from that algebra class you took, is a whole number, with no fractional part. Integers are the numbers you count on your fingers, And that's what they're very often used for in C, as well.
So in a nutshell, we are declaring a variable, which is a place to store information. It's type is integer, which means "whole number", and the value that we're assigning it is 6. That's one line of code.
But it's not very useful, is it? We really need to do a series of things, one after the other, and then tell the user what we've done. That would be a little more useful.
So we'll add a few more statements. C will read our lines of code sequentially, one after the other, just like you are reading the sentences of this lesson one after the other. Like the following:
int favoriteNumber = (3 * 4) / 2; favoriteNumber = favoriteNumber + 2;
Now we're telling the computer to do two things, one after the other. The first line, as we know, sets favoriteNumber to be 6. The second line uses favoriteNumber and adds 2. Note that it looks similar to our previous line: it has a semicolon to end it, a variable name to store it's result in, and an equation to make the result. But you will note that our equation uses favoriteNumber in it: how does that work? That's not an equation that you'd allow in algebra class.
What you need to understand is that in C, the equals sign is an operator, specifically the assignment operator. It is not, like in algebra class, a sign that the two sides are equivalent. What it does is set the thing on the left (in our case, our variable favoriteNumber) to have the value of the thing on the right (in our case, 'favoriteNumber + 2').
But before it sets the values, it evaluates the thing on the right. This means that it finds any variables and uses their values, and computes any equations, as we saw before. So this line sets favoriteNumber to whatever it was (6) plus 2: after our second line, favoriteNumber is now 8.
The other thing you might notice is that we no longer have an 'int' at the beginning of our line. That's because this line is not a variable declaration, like the first line: this line is a variable assignment. The difference is simply that we are using a variable that we have already defined, and C already knows what type it is, so we don't have to tell it again (Note, however, that all variables must be declared before they are used).
So now we've put two lines together, and we've done two things. These two things are closely related, so we might want to group them together so it's easier to read our code later.
Well, recall that C ignores your extra returns. It is common practice, then, to simply add a blank line to delineate the end of one complete idea and the beginning of the next, just like we use a 'return-tab' to make a new paragraph when writing emails. So, adding our next line of code in after such a break, we get the following:
int favoriteNumber = (3 * 4) / 2; favoriteNumber = favoriteNumber + 2; printf("My favorite number is %d!", favoriteNumber);
This new line looks radically different than our other two lines. It is not an declaration or an assignment, but it does work with our variable. What is it, then?
This line contains a function call. Our line of code here is telling some other code somewhere else (in this case, in the C standard library, which we will talk about in a later lesson) to do something, and providing it some information on how to do that, which we call arguments.
There are a few things to note about this syntax. The function is referred to by name, which in our case is "printf". This function prints some text out. After the function name, we have some parentheses surrounding some other information. The parentheses tell us where our arguments start and stop: the information inside them are the arguments that we are passing to the function.
But what are these arguments, and what do they mean? Our first argument is a new type to learn: it's called a string (this is actually a C string, named after the language. There are many other types of strings). It's called that because it's a list of letters that have a certain order, as if they were all tied into place on a string. Note that a string begins and ends with double-quotes (For now, that means that we can't put double-quotes into our strings because the computer wouldn't know where to end. We'll get around this later). The second argument is the variable we've been working with this whole time.
The arguments are different for every function, because each function needs to know different information. Printf needs to know what to print, but it doesn't need to know anything about your network, so you tell it what to print and nothing about your network.
We have done exactly that: we told it what to print: "My favorite number is 8!". But that's not exactly how we wrote it, is it? What's this odd '%d' thing? This is an example of what the "f" in "printf" is: format. The first argument we passed to printf is called a format string, and it tells printf the general format of how we want something to be printed, but it leaves out some of the important information, like our favoriteNumber variable. printf is actually an odd function, in that it can take any number of arguments: normally, you have a specific set of arguments that you tell the function, and that's that. But printf is a little different.
For each %d in the format string, printf will read one of the arguments as an int, and put in into the string. Whenever it sees %d in its format string, it expects to have the next argument be an int. If it's not, it's an error. Hence, the %d is called a conversion specification because it specifies what the type is that you are converting to output. If you want to print other variable types, you use other conversion specifications.
So since our format string has one conversion specification for one int, we give printf one argument after the format string: our variable, which is type int.
Now we have three lines of code, which do three different things. Since we just wrote them, it's easy to tell what they do. But if this were part of a vast program, we wouldn't just remember if we happened upon it months after writing it. So we want to add a few comments to our code, to document what we've done. The following example illustrates two ways of doing so:
//Computes our favorite number int favoriteNumber = (3 * 4) / 2; //is anyone's favorite number not an int? favoriteNumber = favoriteNumber + 2; /* now let's tell the world what our favorite number is! */ printf("My favorite number is %d!", favoriteNumber);
There are three new bits in our code here: the first is a new line at the top that starts out with two slashes. This is a newer version of comment that was only just added to C in the 1999 update. It is a single-line comment, which starts at the slashes and ends with the first return.
The second new piece is on the second line, where again we see the slashes. This is the same style as the first, but illustrates that the comment starts at the slashes, and does not include the code before them.
The last begins with the
/* on the fifth line and ends
*/ on the sixth line. This is a traditional C
multi-line comment, which begins with
/* and does not
end until the next
*/, regardless of what's in
Whichever style you use, the important thing is that comments are completely ignored by the computer. Whatever is inside is for human readers only: they tell programmers what the code does, and why the code is as it is. Comments are incredibly important, and forgotten far, far too often. When you write code, comment it well: you will thank yourself later.
This concludes our first lesson, and we've made quite a foundation. We've discovered how to write a line of C code, how to define and set variables, and how to call our first function. We've also learned how to document our code so that we can reread it later. We haven't done anything with our code, because that is a lesson in itself, which we will cover in our next installment. If we have some room, we will also have a look at creating our own functions.
Seth Roby graduated in May of 2003 with a double major in English and Computer Science, the Macintosh part of a three-person Macintosh, Linux, and Windows graduating triumvirate.
Return to Mac DevCenter