Showing posts with label mac. Show all posts
Showing posts with label mac. Show all posts

Thursday, May 14, 2009

Hello World

Okay, so I think I've downloaded everything properly, but there is only one way to find out.
We created a program called hello world. 
(I called mine hello worlda, since this is not my first hello world).



You should see on the top bar, a "run" tab, if you click on it you will get a list of functions. Open the console function.  The console is where all the magic of our programming happens.  This is where you see the fruits of your labor.  

With any luck you see a screen that looks like this.
















Now to test if we had install everything properly, if you click build and go you should see on the console.  (session started)  and the magical "Hello, World!"  Yessss, we did it!!! But wait, that is actually the default program, so you'll always get "Hello, Word!".  We actually just cheated to get our first hello world.  So lets go into the code and see what is happening.  And actually make hello world from scratch. 

On the left hand side you see the project folder with hello world, you'll see the source, documentation, and products.  Well, I can tell you I know next to nothing on most of those.
Click on source and you'll see a file name main.c, that is the meat of our first program.
Below the file name, there is a window, called the editing window.  That is where the code we will write goes.  

the #include form what I gather is like a set of rules for the program you are about to make.   So the computer knows what rules it is going to follow.  I may not be saying this right, but I'll edit this in the future if I can think of a better example.

The next part is the int main.  This is what c programmers called a function.  A function is basically the task that you want the computer to do.  So therefore the int main in this case is the task of writing hello world.  

Now we have to tell the computer how to write hello world and where.  This is were another function printf comes in.  Well, I consider it a function because it is tell the computer to do something.  printf tells the computer that you want to print something on the console.
you printf from int main by brackets.  Programmers love brackets because this keeps the computer for getting confuse.  It is essential in keeping the computer focus and on track.  

To use the printf(). We have to write what to print.  We put what we want to print inside the printf brackets.  We insert what we want to say in between two quotations marks.  The computer knows that anything in quotations will be the actually saying.  

So lets delete all the things after int main, and see if we can get hello world, from scratch.
we'll put curly brackets { printf ( "hello world\n");  return 0;.  why the \n? well that tells the computer to return the cursor to the next line.  You may also wonder why did we put ; at the end of printf?  the ; is actually very important.  From what I read it is like putting a period at the end of a sentence.  If you do not have ; the computer will get confuse very easily.  

Lastly, what is this return 0? (that's return zero, not o.) That is to tell the computer everything is done with printf and return the control back to main.  It's like telling the computer, you've done the task, good job.

With any luck you won't have any nasty syntax errors.  Syntax errors is the computer telling you that your grammar is wrong.  It does not understand what you are trying to say.  Not the end of the world.  Usually I find I might have forgot a ; or a closing bracket somewhere.

YEEESSS, we did it, the console this time reads "hello world"!!.  We wrote a program from scratch and the computer understood us.  I would pat myself on the back if I could, this is actually pretty good for someone with no programming experience.  The first program that all programmers write is done.  

So recap, we learn how to create projects in c, learn that to get the computer to do something you need functions.  Functions are the task that you want to do.  We learn about the printf, the all import ; and return 0.  Not bad, that is the very very basic things in c.


Nex: Onward to variables and operators.  


Let's Get Started

Well, lets start off with the most basic of questions, why do we need to learn a computer language to program a computer?  Well, from what I read so far, a computer only speaks in 1 and 0's..like this 0100101001010101110.  Humans have a really hard time speaking like that, though there are some people who can actually interpret those 1's and 0's.  There for, we need an intermediary, which is where programming language comes in.  The language that you decided to work with will translate what you type or want the computer to do, to those 1's and 0's.  

I'm learning to program on a mac, with the long term goal of making a iphone app.  Therefore I made myself a road map of learning the most basic of language, with is C.  Then we'll move on the object-c, and finally to cocoa touch or iphone SDK.  Why learn C, first when I can go straight to iphone SDK? Well, it's a lot like learning to run, you first have to take the baby steps, and that is what C is.  Once you get a good understanding of C, I'm told you can apply it to multiple languages.  

What is C?  Well, from  what I gather, it is  a standard language develop by some really smart programmers.  Since it is an establish standard, you can program in C on Mac, PC, or any other computer and the computer will understand you.  I will try to write it so that anybody who has no programming experience can follow (like myself).  By showing the steps, hopefully I can understand things better myself.  I always felt you really do not know something unless you are able to teach it to others.  

Since I've read the first book, Learn C on the Mac, I'll tell you what is needed.  Obviously, you need a Mac, but more specifically an Intel Mac if you want to program for the iphone.  I bought an iMac for this purpose only, but you can actually do this on the mac mini or one of the cheaper mac books.  I picked the iMac 24 inch because of the large screen.  The large screen area makes having multiple items open and all on one screen a pretty time saving ability.  Next you need to download from the apple store the iphone SDK.  You can download that for free and it comes with some pretty neat programs.  I elected to sign up for the iPhone Developer program for 99 dollars. Why? Well, it allows me to actually put my program on my iPhone and see how it runs and getting an app into the app store will be our long term goal.  

The iphone SDK comes with the programming tools you'll need to program.  For the Mac the main programming tool you need is Xcode.  Some macs have this preinstall already, so check your mac, to see if you have it.  It should be in your Hard Drive folder under developer.  Then check the applications folder and it should be an icon with a hammer on it with a blue blueprint in the back ground.  For C programming that will be where we'll spend most of our time.

Xcode when you click on it, a screen pops up that says Mac os x resources and Mac Dev Center.  Play around with this since this a great resource for understanding what the programming language can do and it can direct you to answers of questions you may get later on.  You can close that window.  Next your your apple icon on the top left screen when you click on the Xcode, You should see File, edit, view, project, build, run design, scm, window, and help.   I don't know what all of those are use for just yet.  But if you click on File, a window should popup, and you will select new project.  

New Project.  A window pops up will a list of types of programs you want to make. I do not know 99 percent of those.  Hopefully, I will know those someday.  In the Mac os X section, I select command line utility, you should see an icon, called Standard tool.  That is where, You and I will be writing our first codes.   Double click or select that icon and you will see that it brings up a pop-up window and ask you what you want to name this program.  So far everywhere I've read, says you'll start out by writing a small program call "hello world". I elected to keep that name and keep the tradition going.   It will also ask you where do you want to store your program, I elected to keep it in the documents folder.   

So far so good.  We learn how to create a program.  Next step is learning the ins and outs of this first program