C is for Cocoa
by Seth Roby07/22/2003
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.
|
Related Reading
Learning Cocoa with Objective-C |
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
value (3*4)/2 = 12/2 = 6. This
line of code could be translated to 'remember that my favorite number
is 6'.
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
with the */ 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
between.
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
Showing messages 1 through 66 of 66.
-
Now I'm learning something, finally!
2010-04-24 18:26:13 c.d.stone [View]
-
Integers and Whole numbers
2003-12-28 12:15:29 anonymous2 [View]
This is a great tutorial, but I'd like to point something out. Integers are not "the numbers you can count on your fingers". Those are called the "natural numbers". An integer is, in face a number with no fractional or decimal part, but it may also be negative. Whole numbers, then are integers starting at and including 0. What makes natural numbers different is that they don't include 0. I suppose you could count that on your fingers, though the result would be a fist.
-
Great job!
2003-09-10 23:32:28 anonymous2 [View]
Perfect introduction.
This is the best article on how to start programming that I have ever seen. (I learned some programming languages years ago on the Apple II, but have never succeeded in programming on the Mac.)
As a teacher for many years, I found that a good understanding of basic concepts leads to faster progress down the road, whereas a faulty understanding of the basics eventually leads to a train wreck where the student finally gets lost.
You gave us what a person new to programming needs to know and nothing more, and that is the way to go with new people. Then give them more details when they have a little actual programming experience under their belt, because they will be able to visualize the context and use of the material at that point.
The glossary is also a big help.
I will be passing the word about this tutorial to friends and acquaintances who new to programming.
-
Great job!
2003-09-10 23:32:00 anonymous2 [View]
Perfect introduction.
This is the best article on how to start programming that I have ever seen. (I learned some programming languages years ago on the Apple II, but have never succeeded in programming on the Mac.)
As a teacher for many years, I found that a good understanding of basic concepts leads to faster progress down the road, whereas a faulty understanding of the basics eventually leads to a train wreck where the student finally gets lost.
You gave us what a person new to programming needs to know and nothing more, and that is the way to go with new people. Then give them more details when they have a little actual programming experience under their belt, because they will be able to visualize the context and use of the material at that point.
The glossary is also a big help.
I will be passing the word about this tutorial to friends and acquaintances who new to programming.
-
i think i read something like this on http://www.codon4.com
2003-08-19 19:07:29 anonymous2 [View]
i think i read something like this on http://www.codon4.com
-
i think i read something like this on http://www.codon4.com
2003-08-19 19:07:29 anonymous2 [View]
i think i read something like this on http://www.codon4.com
-
Way to go!
2003-08-16 08:52:24 anonymous2 [View]
After reading all the posts, all I can say is "Great Job!" I have been struggling with C for 5 years off and on, have bought and read through a number of c books, but ALL OF THEM assume things that you aren't assuming.
I have been fairly successful in RB, and even in Cocoa, but have felt the pains of C and the books make so many assumptions, I have missed the point trying to figure out the basics. These are the things you spend 5 seconds on when there's a programmer or teacher next to you that you can ask questions like (why is the header there in the first place? and why is it in a separate file sometimes and not in others), but can keep you from figuring out an example code snippet if that answer has never been provided.
By getting to these assumptions, the reader will be equipped to then read and perhaps understand and see how they can use C&R if they want to. This gets them building something that can excite them enough to make them scratch their heads with C&R. All I would hope for is a daily instead of monthly article, not less detail. The level of assumption is perfect, just bring it more often. Like many have said
-
Maybe you should simply recommend particular books for people to learn C from
2003-07-31 08:23:22 anonymous2 [View]
I do not mean to be terribly harsh, but the fact that this article qualified as an article blows my mind. I am a senior in Computer Science at LSU where we have learned C from day 1 and used it ever since as our program is more of a system development program. There is so much more that these people will need to know that you cannot possible fit into even a moderate number of articles.
Here are some books that I would recommend for anyone who is interested in learning C and possible to go a bit deeper in design:
The C Programming Language (authors homepage: http://cm.bell-labs.com/cm/cs/cbook/)
"This book is written by the developer of C, Dennis Ritchie. Every programmer that uses C has/should have a copy of this. It is filled with useful information, realistic examples, and concise."
C How to Program (ISBN: 0132261197)
"This is my first C book that was really good. I didn't care for some of the examples whenever I got later in my data structure courses, but it is worth its weight. However, please make sure to order the 2nd edition as the 3rd edition is half C++ and excludes the library of functions and descriptions."
A data structures book in C wouldn't be bad either, however my copy is at home, so I can't recommend one. -
Maybe you should simply recommend particular books for people to learn C from
2003-07-31 10:43:00 tallama [View]
Hi, this is the author.
Yes, I agree it would be wonderful if everyone who wanted to program could go to college and learn all this stuff as they worked their way to their CS degree. It would also be wonderful if the texts you mentioned were understandable for those people who don't already have CS degrees.
The idea here is to get people to a point where they can poke around a bit and discover things on their own. I am a firm believer that the best way to learn how to do almost anything on your computer is to read/be told/whatever the basics, and then explore and learn the more advanced things as you go. It's not holistic, but then what is, really?
If these tutorials don't help you, that's fine; don't read them. But from the other comments here I hope you can acknowledge that many other people have been looking for just such an approach, and that's why this tutorial is a good idea; people want it. If you're not one of those people, that's fine, but that doesn't mean that no one wants it. (Don't fall into the common CompSci trap of believing that everyone's needs and wants are yours). -
Maybe you should simply recommend particular books for people to learn C from
2003-08-29 15:00:09 anonymous2 [View]
I liked this article, but no newer ones seem to be arriving or a book being written on the same idea. Maybe I bookmarked the wrong page (The first C is for Cocoa page) and can't see easily the follow-ups.
Are new articles and-or a book being written around this. I found it funny when I originally saw the article (the day it came out, too) that I had been griping for a year for precisely this fact. I had understood that it was universally understood that to learn Cocoa you had to learn C first. So I started trying to get a handle on C, but I did start reading in Objective-C and Cocoa and for half of the things I read for C I'd read something in the Cocoa documentation along the lines of "In C you would do this, but in Cocoa it's not necessary". Also a lot of the C don't assume you'll be using frameworks and libraries for a graphic environment, so I was able to make a little C program that would print some stuff to the screen in text but that had advanced me exactly 0% in transforming that into a Window in Cocoa that did the same thing.
Eduo
eduo@mac.com
-
C for the masochists
2003-07-30 00:58:07 anonymous2 [View]
Compared with Perl and Java, C is a difficult language because of the need to manage memory manually and fiddle with pointers. If you are a casual programmer, I would recommend that you stick with languages with automatic garbage collection such as shell/Python/Ruby/PERL/Python/AppleScript. Furthermore, you will probably write more programs in these languages than in C because of their built-in features (regular expressions, etc.)
If you are really masochistic (:-), I would recommend the following two books:
Advanced C Programming by Example Authors John Perry ISBN 0534951406 Publisher PWS Publishing Co. A difficult book (lots of pointers!) but very rewarding.
Interprocess Communications in Unix, by John Shapley Gray. An excellent book on Unix programming in C. -
C for the masochists
2003-08-02 16:17:52 retro [View]
The idea behind this article, as I see it, is to teach people enough C so that they can use Objective-C and Cocoa. Learning a language with automatic garbage collection would be pointless since Objective-C does not have automatic garbage collection. However the memory management is different and simpler than straight C; you don't have to deal with malloc and free. Also, there is less fiddling with pointers.
-
It's about time!!
2003-07-28 15:11:35 anonymous2 [View]
I can't tell you how long I've been waiting for this article!
Please Seth, don't make us wait too long for the next installment. (And as a token of appreciation to your earliest fans, a discount on your book when it is published!)
;)
-
Excellent!
2003-07-28 12:52:01 anonymous2 [View]
Thanks for the easy start.
How about a Tutorial for Applescripters to Obj-C?
We already know the structures of a language but need help in translation of concepts from one to the other.
-
Great Article
2003-07-27 17:04:01 anonymous2 [View]
Can't wait for more! Great job.
-
Learning C first and a word of caution
2003-07-26 03:11:40 anonymous2 [View]
Hi,
With all the talk about learning C first, that is fine with me. But I do not believe that there are any books that do teach me how to do that in (what is now called) xcode. I do have books on C and C++, and they soon start talking how things work in the Borland compiler etc. I do NOT want to know that, I want to learn it in the Mac environment I use, and I welcome this series (but hope a book follows).
A kind word of caution to the author: You write very well, making me understand everything you write. But beware: I've seen a good many books which start that way, but when things get harder, the explanations get shorter and less clear. To get us to program, keep up the current clarity.
Yours,
Bert -
Learning C first and a word of caution
2003-07-26 16:31:07 anonymous2 [View]
There's a tutorial for Project Builder at CocoaDevCentral:
http://cocoadevcentral.com/articles/000054.php
I guess you'll be able to find your way in Xcode with that one (basically, just create a Standard Tool project and type in your code into the main.c file...) -
Learning C first and a word of caution
2003-07-27 11:29:43 anonymous2 [View]
Great link. Thanks.
Bert
-
How will new installments be announced?
2003-07-25 02:16:57 hiramnl [View]
I was very happy to discover this tutorial, because just as the author says, an entry into Cocoa that didn't require prior C programming experience was what a lot of people needed. Thanks a bundle! I'd like to be notified, somehow, of new installments. In the O'Reilly mailing list options, I don't see a Cocoa category. Will this be a weekly tutorial? Is e-mail notification of new installments offered?
-
Where is part 2 ?
2003-07-24 14:50:05 anonymous2 [View]
Some of us will pay for this type of help..... -
Where is part 2 ?
2003-07-24 21:39:48 anonymous2 [View]
Does anybody know when Part 2 will be released? I'm afraid of missing the next installment. I just don't know what I'd do. If I were to miss it I might just start a spiral downward that could ruin my life. When? Somebody tell me! -
Where is part 2 ?
2003-07-24 21:14:45 tallama [View]
If you feel like it, contact me by email and you can send me some cash. ;)
-Seth A. Roby
-
This article is an excellent idea for a book
2003-07-24 05:00:51 anonymous2 [View]
You should write a book, called Programming Objective C. That's the book that's missing from O'Reilly's OS X library: One that isn't focused on teaching you how to use Project Builder and such, but concentrates on teaching the basics of C, then the Objective C superset.
Examples should be made using XCode (of Panther), complete with screen shots, so that newbies could see how to compile and run from this standard environment etc. Basically the examples should be of small command line utilities, because if you try to explain Interface Builder at the same time, the book would be all too big. You could simply point to other O'Reilly books on that particular subjects, as reccommended "what do I read now" books.
You could use an appendix to give a quick introduction on how to build a user interface in Interface Builder, and how to connect everything together and add some code. Another appendix should explain _how_ to use, navigate and read Apple's Cocoa API documentation. -
This article is an excellent idea for a book
2003-07-24 06:09:51 cothomps [View]
O'Reilly did publish "Building Cocoa Applications" that starts with Objective C basics (assumes C knowledge.) The book starts off with creating an Objective C program in a plain text editor, compiling on the command line, etc. (Just like you would when programming in C itself)
The book then Introduces Interface Builder, etc. It might not be exactly what you're looking for, but it might come close. -
This article is an excellent idea for a book
2003-07-25 04:56:50 anonymous2 [View]
No, it's not what I'm looking for. As I say, if it were up to me you could skip the Interface Builder part completely (except for (perhaps) a -very- brief introduction in an appendix).
Also, what I want is a book that does _not_ assume knowledge of C. Most fresh programmers today know something like Basic or Java, or some sort of scripting language, but not C. So this should be a Programming C and Objective C book, with focus on makin small command line applications.
All the other stuff comes in the other books. -
This article is an excellent idea for a book
2003-07-25 08:32:18 anonymous2 [View]
I agree. I know C++ and Java, and while I've been told many times that if you know C++ you can figure out C, it'd be much easier for me to just start at the very basics, like this article, and build up. In a book, I'd probably just skip the first chapter and relearn the rest.
-
Yes, gimme more!
2003-07-24 04:00:10 anonymous2 [View]
Next episode, please!
-
Can I use objective C in Windows?
2003-07-23 22:53:08 anonymous2 [View]
Is there an objective C compiler for MS Windows? I'm afraid I'm stuck with it for the time being.
Thanks. -
Can I use objective C in Windows?
2010-07-20 15:54:32 cleverapps [View]
If you just want to experiment, there's an Objective-C compiler for .NET (Windows) here: http://www.qckapp.com/index.html?p=ObjC -
Can I use objective C in Windows?
2003-07-29 06:02:59 retro [View]
You can use Objective-C with one of the compilers already mentioned, but that alone is not going to allow you to build "Cocoa" applications per se. "Cocoa" is a "framework" or set of prewritten classes and methods (in C they call them "functions") that is unique to the Mac OS X platform. Unfortunately, you will not be able to program applications that use Cocoa from your Windows box. -
Can I use objective C in Windows?
2003-07-29 08:03:17 tallama [View]
Hi, this is Seth A. Roby (the author).
The answer is, as most answers are, more complicated than a simple yes or no.
Objective-C can be compiled by gcc, which means that you can use it pretty much wherever.
However, as Retro says, the main strength of Objective-C in OS X is the frameworks. But Retro's a little off when he says that those are available only fro OS X.
But OS X is not where these frameworks originated; for that we have to go back through OPENSTEP, where NeXT and Sun worked together on the frameworks and made them an open standard, to NeXTSTEP where they were originally invented.
And OS X is not the only place that took the frameworks from OPENSTEP; GNUSTEP (gnustep.org) is making an OPENSTEP-compliant code base that can be used by anybody (it's even under the friendly Lesser GPL). GNUSTEP works pretty well (I'm told) for Foundation (low-level) classes, but the AppKit support is spotty at best. GNUSTEP works pretty well on Linux and newer FreeBSDs, but elsewhere they generally report themselves as unstable (see http://gnustep.org/information/machines_toc.html). The big deal, though, is that GNUSTEP is going to be playing catch-up with Apple as Apple extends the classes available, which Apple has been doing and GNUSTEP has pledged to follow to the best of their ability.
So the short answer is 'yes', but the longer answer is 'mostly.' Objective-C is very much available, and the frameworks are making their way there, but the support is not quite there and the latest and greatest will always be OS X only. Unless, of course, Apple re-releases Yellow Box for Windows. I expect that sometime after the return of Enterprise Object Frameworks, which should be sometime after armageddon.
- Seth -
Can I use objective C in Windows?
2003-07-24 07:54:45 anonymous2 [View]
Yes. Check out DJGPP at http://www.delorie.com. -
Can I use objective C in Windows?
2003-07-24 00:17:06 senjaz [View]
While I can't answer that question with a definite I'm reasonably certain: yes.
Firstly Obj-C support is built into GCC which as a cross platform compiler should run on Windows.
GNUStep is an open source effort to clone the old OpenStep frameworks which became Mac OS X when Apple bought NeXT. See http://www.gnustep.org/
You might need to use cygwin (http://www.cygwin.com/) which is a set of a dll and other tools to allow you to compile and run otherwise UNIX only programs.
I know this is the Mac OS X section but it's a worthy question, and so I for one would be interested to know how you get on.
Senjaz
-
This made me install the dev tools
2003-07-23 20:10:14 anonymous2 [View]
Thanks. Excellent article. Looking forward to lesson 2.
I had been hesitant to download the developer tools until now. This article convinced me to give it a try. -
Re: This made me install the dev tools
2003-07-23 21:04:40 anonymous2 [View]
If you're anxious to get Mr. Roby's little sample to compile and run, let me give you a little nudge.
Getting a C program to run involves three steps:
1. Create your C source file. If you've never used vi or emacs then I would recommend pico. Save the source file as 'lesson1.c'.
2. Compile it into machine code:
gcc lesson1.c
3. Run your program and see your happy result:
./a.out
I'm not going to go into how or why any of this works; I will let the excellent tutorial run its course. I will, however, say that despite what you've read, the line that begins with '#' (it's not a comment) must remain as an independent line.
Here is the entire program:
#include <stdio.h>
void main()
{
//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);
}
-
Re: This made me install the dev tools
2003-07-23 22:17:35 anonymous2 [View]
Presuming somebody interested in this tutorial is going to be working on a Mac, why would they bust out vi or emacs or pico?
You can do all your typing in Project Builder. It's a perfectly servicable text editor. Or use BBEdit / BBEdit Lite. Much friendlier. -
Re: This made me install the dev tools
2003-07-23 21:07:56 anonymous2 [View]
Whoops! I should have tested the program before I hit 'Submit'. Here is the revised code:
#include <stdio.h>
int main()
{
//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!\n", favoriteNumber);
}
-
Re: This made me install the dev tools
2003-07-26 02:00:07 anonymous2 [View]
misssing
return (0);
are we? :) -
Re: This made me install the dev tools
2003-09-03 19:48:45 anonymous2 [View]
Not really. return 0; is optional in standard, modern C++ - and by extension, I'd assume the most recent C spec.
Though gcc accepts some C++ syntaxes in C-compiling mode.
-
Why not avoid ObjC?
2003-07-23 19:13:26 anonymous2 [View]
Java, AppleScript(gag), and now python can be used to create cocoa apps. I think there is a ruby bridge as well.
I haven't done anything more than the examples, but pyobjc seems to work very well.
http://pyobjc.sourceforge.net/ -
Why not avoid ObjC?
2003-07-23 21:18:30 tallama [View]
Hi, this is Seth Roby.
I admit readily that Objective-C is not the only path to Cocoa development, and I think that that is a wonderful thing.
However, Objective-C is definitely the best documented route, once you get to the place where you're consulting the documentation. Aside from that, it is an elegant, simple language with the power of C behind it and the breadth of Cocoa in front of it.
But that doesn't mean it's the best language for all situations. If you need to use a bit of Java code, then by all means use Java; you can still use Cocoa (albeit a bit slower, and without a few functions).
If you're an AppleScript wiz, AppleScript Studio can be a wonderful way for you to make the leap from helpful little bits to helpful big bits. And nothing can beat AppleScript when you need to do dynamic interapplication communication between applications that don't know each other.
And I continually hear wonderful things about Python and Ruby. PyObjC sounds very promising, but alas I haven't had time to try either of these languages. Both are on my to do list.
But the bottom line, I think, is that Objective-C is a wonderful middle road. It allows for the bit-twiddling that C is king at, and the Rapid Application Development that Cocoa is wonderful for. With Panther's new Binding features built into IB, I think Objective-C has a bright future in front of it, as our controller code shrinks into the distance of memory.
But whatever you end up using for Cocoa programming, and as has been said in some of the other comments, C is really the grand-daddy of almost every language that's ruling any roost right now. Learning C will help you learn almost any other language you want to pick up, as well as get you some really great experience in computing in general.
-
Great
2003-07-23 17:13:23 anonymous2 [View]
Great work. Please give me more. This is just what I have been looking for.
-
C/Obj-C/and blasted OO
2003-07-23 14:58:00 anonymous2 [View]
I agree with your first paragraph very much. Though I have been using C for several years and still hit a wall. I simply loath OO programming. Though ObjC.pdf on the Mac Dev site has helped a bit for learning OO particularly with ObjC.
What I'd like to see are:
1.) A well detailed,easy to read listing of the objects/methods in the Cocoa API. NSWhatever get's confusing, especially when several basic components are still not available on the Mac website.
2.) A good, C->Obj-C tutorial. This seems to be what you're doing and I'm grateful. I'm just looking forward to seeing the transition period between the two.
3.) Is it possible to do this all from an editor like emacs?
IPBuilder is wonderful, but with the constant, click here-> drag to this element, drag back to this method, etc etc it get's really confusing. Guess I'm just so use to cli programming.
Well I'm done, thanks again for the article and keep up the good work. -
Re: C/Obj-C/and blasted OO
2003-08-01 15:29:40 halliday [View]
You ask for "2.) A good, C->Obj-C tutorial." Does this have something to do with your statement about how you "simply loath OO programming?"
When you say "I simply loath OO programming" I wonder whether the issue may be the difference in the paradigms of Object Oriented vs. procedural programming? OO is a very different way of thinking about programming. Yes, you can, ultimately, accomplish all the same ends with either (C and any OO languages are all Turing equivalent, after all). However, just as with any language comparison, there will be things that will be much easier to accomplish (or, potentially, even grasp, mentally) via one paradigm (world view) vs. another.
Interestingly, I have found that for many people, those that have not had prior C (or other procedural) programming experience before learning about OO have a far easier time. Perhaps it has something to do with the types of people that have tended to learn programming before OO, but I tend to believe it has to do with the difficultly in unlearning old concepts about what programming is.
In any case, I'm glad to see that you have already found the "ObjC.pdf on the Mac Dev site", since, otherwise, this would have been my first recommendation. Another good choice, if you can find a copy, is the old Byte magazine article on SmallTalk (anyone know the date of that article? Sometime in the 80's I believe)*.
* I was actually exposed to SmallTalk, via this article, long before any other exposure to OO. However, just like Steve Jobs' first exposure to Xerox's work failed to impress upon him the true beauty and utility of OO [vs. his being enamored by the GUI], there are beauties of SmallTalk that I only came to recognize much later, after having experienced other OO-like languages. (Can you imagine a programming language that has no native control or looping constructs, other than some textural macro languages, and yet has sufficient power to allow the creation of such constructs by the user [though these are usually included in the standard libraries]?) -
C/Obj-C/and blasted OO
2003-07-24 01:46:29 anonymous2 [View]
"Is it possible to do this all from an editor like emacs?"
If you mean how to programmically build nib files I know you can. If you look into nib files they are just XML, and you should be able to find info on that somewhere.
Nib = next interface builder
-
C/Obj-C/and blasted OO
2003-07-23 15:29:30 anonymous2 [View]
"Is it possible to do this all from an editor like emacs?"
Sure. Heck, I think this is where emacs came from! Don't do it though. cli programming is fine if you're really into it but I implore you to think about that one for a little bit. I mean, if you want to do it in emacs, why not just program in assembly? I'm kidding ;-)
-
THANK YOU!
2003-07-23 14:56:21 anonymous2 [View]
This is _exactly_ what I've been looking for.
You have a comfortable place to sleep and dinner anytime you're in Santa Barbara!
-
Recommended Book for C Programmers
2003-07-23 14:12:07 cothomps [View]
If you are a beginner at C Programming, one of the best books out there is actually an O'Reilly book:
http://www.oreilly.com/catalog/pcp3/
It's one of O'Reilly's older titles, but still one of the best in print. I used this book to train a group of beginning programmers in C programming on UNIX boxes (in that case, using gcc on Solaris). While the seminal K&R book (http://cm.bell-labs.com/cm/cs/cbook/) is fantastic, it can be a bit "dense" for beginners.
At some point, I would also like to see how well the Stevens books translate to OS X.
Of course, I haven't touched C on a full time basis in five years... yipes.
-
For the impatient...
2003-07-23 13:52:40 anonymous2 [View]
...I'd recommend picking up a copy of the canonical C book, "The C Programming Language" by Kernigan and Ritchie (published by Addison-Wesley, but hopefully O'Reilly won't be too upset about me mentioning it here!). It's very small, yet gives you absolutely everything you need to know (and more!) as a foundation for Objective-C. -
For the impatient...
2003-07-23 15:08:17 anonymous2 [View]
I fully second this. And third it. You will be way better off having this book available at all times while learning C. K&R is REQUIRED as far as I'm concerned. If you don't get it now, one day you will and you'll kick yourself. I'm not kidding. It's gonna hurt too! You might think you can get by without it. You're wrong. Remember, you'll be sorry.
-
Good but impractical
2003-07-23 13:14:31 anonymous2 [View]
I am happy to see articles on Cocoa programming for programmers at any level of expertise. Good job! I know people have day jobs that don't involve programming and nevertheless want to learn Cocoa. I think that is great!
I just don't think it is wise to minimize the importance of learning to program in C before attempting serious use of Cocoa. An understanding of C and low level programming issues is important before attempting any serious programming project with or without Cocoa. Frankly, learning everything there is to know about C and the most common standard libraries used with C will take a tiny fraction of the time needed to learn to use Cocoa effectively. I fear that attempting Cocoa without understanding C and low level programming issues in general is going to be frustrating and error prone. Furthermore, a general understanding of computer science is needed even for hobbyists programmers. If this series teaches enough general programming skills and enough C to really use and understand Cocoa, this will be a great series! If not, I worry that people will end up discouraged and won't understand why their programs are slow and buggy.
I hope my concerns are misplaced. I am sure we can all agree that Cocoa is a powerful tool. It is sometimes necessary to learn how to use less powerful tools before the more powerful ones can be used effectively. Impatience aside, I recommend that people really learn C before attempting Objective-C and Cocoa. C is a valuable skill and contributes a foundation of knowledge that is applicable to almost any programming from LISP to C++ to Java to perl.
-
Good but impractical
2003-07-23 16:00:11 anonymous2 [View]
I disagree that attempting serious use of Cocoa before completely learning C is bad. However, I completely agree that the importance of learning C needs more emphasis everywhere in the Cocoa world. It's often sold as being some magical new kingdom unto itself where you can just "get it" and make things happen. You and I both know it doesn't work like that. I don't think your concerns are misplaced, but I think this series is an excellent start to move aspiring Cocoa programmers from nothing to something and maybe entice them to learn more about this craft.
-
just what I was looking for...
2003-07-23 12:18:28 anonymous2 [View]
..."exactly how much C do I need for cocoa" is a question I've been asking for a while now. big question is, what's the schedual for this series? I'd be hopeing a couple a week if all the articles are paced as this one is. (That being said, it looks like a great start)... -
just what I was looking for...
2003-07-23 15:42:47 anonymous2 [View]
"exactly how much C do I need for cocoa" answer: Exactly all of it. Seriously, objective C is not another language, it's a superset of C. It ADDS object oriented features to C. Objective C is not a redefinition of C, it IS C with some new features. IOW, you need to know C. C, C, C, C, C!!!! Si! I mean yes! No good shortcuts to this one I'm afraid. That said, there ARE powerful things you can do with Cocoa knowing only a limited amount of C and it's Obj C addendum. Learn a lot of C, it's not all that hard. -
just what I was looking for...
2003-08-01 16:41:23 anonymous2 [View]
So C is procedural? I was always under the impression that it was OO...... -
just what I was looking for...
2003-07-24 01:59:28 anonymous2 [View]
If you are new to the C set of languages, and you want to learn to do something cool, you don't need to be a master at C. I agree that in the long run, having a good understanding of C will be required, but everyone wants to jump in and start writing programs that they are familiar with, with windows and menus, not just a command line tool that prints "Hello World".
People want to do fun, practical things when they program, and telling a new programmer (or someone coming from visual or real basic) that they need to write command line tools for a while is really going to discourage them.
Start writing in Cocoa, but be prepared to have to learn a lot of C in the process. Remember, this tutorial is aimed at getting people to start coding, and I don't think programming in pure C is a necessity to learn objective-C. -
just what I was looking for...
2003-07-25 21:58:20 anonymous2 [View]
This is the si! programmer from just above. Well said.
-
Thanks!
2003-07-23 10:46:08 anonymous2 [View]
As a beginner with no lower level programming experience, this series is shaping up to be absolutely perfect for me. Having used Applescript Studio for some time now, I felt eager to move to Cocoa, but was reluctant to teach myself C because I could not foresee using it frequently enough to become comfortable with it quickly (can't give up the day job). This feels like the right pace and approach - please keep 'em coming! -
Thanks!
2003-07-23 15:51:00 anonymous2 [View]
Stop being reluctant to teach yourself C. The truth comes out, you have to learn it to use Cocoa. You will use it a lot and learning it quickly is not an option, it can take time. It's well worth the effort.
-
Good idea... but...
2003-07-23 10:21:46 arkham999 [View]
I think this is a really useful idea. However, at this pace it could take months to get to the point of a simple GUI Cocoa app that does anything consequential.
Maybe when you get to that point, you could offer the articles together in PDF format or something, rather than having to read the articles in distinct sections. -
Good idea... but...
2003-07-23 17:29:33 tallama [View]
Hello, this is the author speaking.
The idea here is not necessarily to move the person from a complete newbie to the president of the next Omni Group. The issue these articles are intended to address is that every tutorial out there assumes that you know too much. Granted, as many people here are pointing out, knowing all that they ask you to is a great thing, but it's not REALLY required. You can get by on less.
How much less is the question. and that's a matter of opinion. The basics of C aren't that hard, but they are a formidable obstacle if you're coming at it with no previous knowledge at all. This article is the start, and it's intentionally aimed at the lowest point I thought I could pick people up at. From here we'll move on to bigger and better things, and try to give people a bit of ground to stand on. But we're not going to be building a mountain out of it; there are tutorials that do that work already. This is merely an attempt to bridge the gap that I saw in the learning curve. -
Good idea... but...
2003-07-23 11:03:41 anonymous2 [View]
... or write a book! ;-)
-
EGAD MAN!
2003-07-23 09:58:12 anonymous2 [View]
Write a book on learning it!!! : )
-
Excellent!
2003-07-23 08:00:47 plumcreek [View]
Finally! A series that teaches Cocoa without assuming a bajillion years of C experience.
Thank you!
-
A well-written start...
2003-07-22 21:16:54 anonymous2 [View]
Were I a beginner, this is the kind of tutorial to look for. You explain things well and I think your column, should you choose to continue, will be a boon to those with no previous C knowledge and an excellent starting point for Mike Beam's articles.
-
I think this is a good thing you are doing
2003-07-22 20:49:37 anonymous2 [View]
I had this problem when I first started learning cocoa. I think this is an excellent Idea. Keep up the good work!










Anyway, after spending 15mins reading your first chapter here, I learned more Cocoa -- starting with what the "C" stands for -- than in the 12-hours of Apple tutorials. Thanks a lot for explaining the basic syntax and terminology. I can't wait to read through the rest of this. If I ever get my product to market, I'll be sure to remember you.
c stone