Tuesday, October 12, 2004

Obj-C: My First Program

I'm gonna learn this by trial and error. So let's get programming.

I'm getting most of what I'm doing in this entry from the C Language Tutorial for Cocoa by Scott Stevenson. That's being supplemented with things I pick up via Google and other places.

The way to go on OS X seems to be Objective-C with a Cocoa interface. And to know Objective-C, everyone seems to think you need to know good old C. Well, I don't know C and I don't feel like learning C. So I dug around the net a little and found some pages that seem to solve the problem; they only present the C you need to know to learn Objective-C. So this is where I'll begin... basic C for the purposes of learning Obective-C.

The following is a version of the ubiquitous "Hello world!" program that everyone seems to make as the first program in every programming language.

Note that the line number included in this program (and in future programs) is only there for reference purposes and is not a part of the code. Putting it in your code will screw it up.

1   #include <stdio.h>
2
3   main ()
4   {
5      printf ("Gee willickers, this is snappy.\n");
6      return 0;
7   }


Line by line...

1. This line gets the printf function from within the built-in stdio.h C library.

Think of the #include statements like a pocket dicionary. When you're shopping for shoes in Spain and you don't speak Spanish, you open up your pocket dictionary to the clothes shopping section and look up "How much do the shoes cost?" Likewise, when you're seeking an input-output function (which is what's in stdio.h) that's part of the built in C functions (built-in C functions are marked off by < and >), you open up stdio.h and look up printf, which is the input-output function we're looking to get here.

2. This is just a space to make it look pretty. C will ignore line breaks so feel free to space as suits your eye.

3. This is the main function, the only one in this program.

All C programs need the main function. This one only does one thing; triggering the printf function.

The () following main contains parameters. As this function doesn't contain any parameters, I'll get into those later.

4 & 7. While parameters go between the (), the rest of the function goes between the {}. Again, space as it pleases you.

5. Here's the big function of this program: printf. What printf does is print whatever is in the parenthesis after it in the default window. As we'll be using Terminal to run this code (more below), that default window will be in the Terminal window.

Anything in between "" will be published as is. However, anything following \ is a special character. In this case, the \n represents a non-printing line break, which gets us a new line. The most common are:

\t    tab (ASCII 0x09)
\n    line feed (ASCII 0x0A)
\r    carriage return (ASCII 0x0D)


I'm not really sure what the difference between \r and \n is, except that Unix (and I assume OS X) uses \n.

6. For some reason C always wants you to return an integer. return 0 "returns" a 0, which is the customary thing to return to indicate a successful completion. I'm not sure where this 0 is going, but all the experts seem to think it's necessary.

And don't forget the ; at the end of each function line that is between {}! C will get all screwed up if you do!

To run this program, we're gonna need Terminal. I'm testing this on Terminal 1.4.3 (v100) on Mac OS X 10.3.5. So here's what you need to do.

1. Save the above code as program1.c. The ending .c is *gasp* the suffix for C code. The name program1 is just a random name I chose.

2. Open up terminal and type in gcc -o program1 . Don't forget the space after program1.

GCC is the compiler that Terminal and Xcode use to compile C code. A compiler makes your program work, and that seems to be all that I need to know right now.

The command -o tells Terminal to name the command whatever follows it, in this case program1. This does not have to be the same name as the .c file but I've made it so for simplicity sake.

3. After typing that but before pressing return, drag the file program1.c onto any part of the terminal window. This will put the entire path right on the end of what you just typed. You could always type it all out, but this way is just so much easier.

4. Press return.

5. Type in ./program1. The ./ tells Terminal to look for something in its short term memory. program1 tells Terminal the thing it should be looking for in its short term memory - the program1 that we just made.

6. Press return again. You're results should come up.

The input and output of your Terminal session should look something like this:

gcc -o program1 /Users/vincentpace/Desktop/C/program1.c
./program1
Gee willickers, this is snappy.


If you got any error messages, double check the code. Pay attention to the little details, because one typo can throw the whole thing off kilter.

So congratulations. You just made a program.

Links to this post:

<\$BlogItemBacklinkCreate\$>

,

<< Home