# Programing for beginners



## ziomatrixacs (Dec 8, 2008)

I know, I should probably take a computer programing course for this but I want to do this on my own time/budget for a hobby.. I was thinking about getting into programing with straight C (not C++) and basically... where do  I start?  Id just like to see what its like, and to see if I am any good at it. I know a little about C and have a book called "The indispensable guide to C" But I could always use more help. Im really not sure where to start typing the program, and that book is geard towards programing micro computers so some things arnt the same. (like it using LEDs on the micro computers carrier board, but my mac probably doesnt have the same names for the LEDs)


----------



## nixgeek (Dec 8, 2008)

I've been thinking about getting into Python programming myself.  There's a free (as in speech) manuscript called _Think Python_ that I was referred to by someone on the IRC channels I frequent.

http://www.greenteapress.com/thinkpython/thinkpython.html

Since you asked about programming in C, you could always check out the stuff that GNU has available for their compiler to get you started.

http://www.gnu.org/doc/gnupresspub.html

I believe Apple might also have something if you're going to be using Xcode.  You could check at their developer page to see if there's anything.

http://developer.apple.com/


----------



## ElDiabloConCaca (Dec 8, 2008)

I would recommend using nothing but the Terminal to learn how to program C, as firing up XCode and trying to create a C program will just confuse most people.  It seems as if you need some basic Terminal knowledge before you begin programming, though, specifically, but not limited to:

- How to navigate your file system and view directory listings in the Terminal (ls, cd, etc.)
- How to create and delete files (cat, rm, etc.)
- How to create and remove directories (rmdir, rm -rf, mkdir, etc.)
- How to create text files (vi, vim, nano, etc.)
- How to compile programs (cc, gcc, g++, etc.)
- How to execute the compiled programs (the dot-slash operator, etc.)
- How to modify your $PATH variables

This is what made me worry and think that:


> Im really not sure where to start typing the program...


Any computer program is a text file, plain and simple, until you "compile" it.  You would "type" the program wherever you would type plaintext normally, whether that be with TextEdit or some UNIX-y style Terminal text editor.

I would start off with a good book on UNIX/Linux terminal navigation _before_ learning C, simply because you need to.  You'll actually learn a bit of C in the process, because UNIX and Linux are written in C, and a lot of command line programs and terminal commands are actually directives and functions in the C programs you'll be writing, as well.

While you can learn C via XCode and without much Terminal knowledge, the Terminal and C are so integrated together that it would be a back-asswards way to learn how to program in C.

Just a few cents worth of suggestions from someone who has taken the route of learning C in detail.  Without Terminal knowledge, it is going to be hard to understand some of the concepts used in the C programming language.

I do applaud your efforts to learn C, though, as most people skip straight to the higher-level languages (that are arguably easier to learn) like C++ and Java, without knowing much about what they're really doing.  C makes you do _everything_ manually (even manage how much memory you're using), and once you learn C, you'll have no trouble jumping right into other programming languages.

Come back when you reach the section of C programming on "pointers" and we can help break it down into understandable terms -- if you don't need extra care and explanation walking through that part of the language, I'm sure NASA and the government will have six-figure job offers for you in no time...


----------



## ziomatrixacs (Dec 8, 2008)

Honestly I have no experience with terminal. To be frank, I see it as a dark and mysterious monster.  I knew one could type a program into anywhere that edits text, I meant how do I compile it into something that "does something."  I wasnt sure if I should use the term compiler or not there. 

So far, I know this..(or atleast think it) 
C is a lower level language.
C can be used to make some games, in combination with MAYA. (MAYA will kill my ibook though)
You use a compiler to turn the text into a program ( I dont know of any compilers of mac os x)
I had x code on my ibook when I bought it from my friend, she didnt even know it was there but I deleted it. I think its on the OS X installation disc, I can get it with pacifist.
One way to make a program "lighter" and easier is by defining a variable instead of using numbers.

Example:

This line uses 1
This line also uses 1
There is another line that uses 1

If you find a problem and need to change 1 to something, say 2 then you must go through all of the lines and change it to 2.  If you replace 1 with a variable, like A and set A=1 then you can easily change all of the numbers at once.

Thats every thing I know, and I think I have a long road ahead.


----------



## ElDiabloConCaca (Dec 8, 2008)

That is a very basic understanding, yes, with some flaws (using variables does not relate to a "lighter" program).

In your case, you may want to work on understanding some theory before diving in with straight-up programming.  Don't even start on graphics yet -- for the next 2 years, you won't be programming a single graphic-based anything!  

And... learning how to program without knowing some basics about the Terminal is going to be very, very tricky.  Programming is easy, but it's like riding a bike -- you will struggle and struggle and fall and fail, not knowing what you're doing wrong, trying to get the "hang" of it all, them BAM! -- one day, your mind will open and things will start to make a LOT more sense.

I would highly recommend some courses in programming for someone of your experience.  If you try and teach yourself, without knowing what you're really doing, it's going to be very difficult to tell if you're using the correct methods, or what, exactly, you're doing right, and what, exactly, you're doing wrong.  You can follow examples in the book, but books don't answer questions that you may have.  Books can't watch over your shoulder and tell you when you're heading down the wrong path, and why that path isn't the most efficient one.

Programming is tough, and it requires a lot of background in math, as well.  A basic understanding of Algebra isn't going to cut it, unless you simply intend to use your programming knowledge to make very, very simple programs.

I think that programming is best taught in a course-style curriculum, and 75% of those courses have nothing to do with computers at all.  "Programming" isn't just about writing code on a computer -- it's a collection of knowledge from different disciplines, including math, science, physics, and yes -- computers.

It's a ton of friggin' work, but I think it very worth it and it's _extremely_ rewarding in a lot of aspects.

I wish you the best of luck, but with what limited information we have about you and what knowledge and level of mathematical education you have, I must suggest taking a different route to programming other than by book.  While some, I'm sure, have self-taught themselves out of a book, I would think that the vast majority of people in your situation ended their programming careers in frustration rather than completing the task and _really_ knowing how to "program" a computer.

Good luck!


----------



## ziomatrixacs (Dec 9, 2008)

Well, Ill give a little back ground info when it comes to academics and relevant stuff.

When it comes to math,  I am a bit of an odd ball. From 1st to 4th grade, I liked math, it made sense. I could visualize it. You have 5 boxes, and you put 2 apples in each box then you have 10. Or, 5x2=10. But once algebra and that &#8220;crap math&#8221; hit me, it was all down hill. Made no sense what so ever. And to be blunt, I didnt give a crap about square roots and pi and rational numbers._I was never going to use that any way.[\i]  Ill admit, from 5th to 12th grade, I passed simply because I showed up and acted like I cared. And on top of that, I was taking all &#8220;basic&#8221; classes. But some how I managed to slip though the cracks in my senior year and ended up taking the hardest course, with the strictest teacher in the school district: Physics. Physics was a course you had to ask for and you must meet a certain criteria and former math teachers must recommend you. I asked my guidance counselor about it, and she wiggled me in some how. Heck, my one math teacher said I should NOT take physics, and wouldnt allow me to take Calculus with her. I ended up with the lower, Pre calc instead. Since its hard to get into physics, only the &#8220;smart&#8221; kids were in that class. No goof-offs, just the kids with real high GPA&#8217;s. It was me, the one who could not rearrange 5=1*x to solve for x and then the kids taking college level calculus. (The school is a 5 minute walk from a college!)  But some how, I ended up passing that class in the top 4% with an A-. How did I get such a grade in such a class? For the first time in years, math made sense. I could visualize two objects colliding, a ball falling off a roof, a cannon shooting over a mountain, I could even apply this to real life situations! 
Ironically, the most intense math course of the SD was &#8220;easy&#8221; to me. I still review my notes ever now and then. Of course I did spend most of my lunch time with my precalc teacher, she taught me some basic algebra. 
How did precalc go you may be wondering? I nearly failed. I only passed because of the work she knew I was capable of. 
So, right now, I think I have the potential to be pretty good at math as long as I enjoy it. I like to think of physics as &#8220;math with a GUI.&#8221; As for those &#8220;pure&#8221; math classes like calc, I suck at it.

Other possible relevant stuff..A while back I used to be big into Legos. Ever since I was One, I built robots and stuck motors spinning rubbing bands to axles powered by a 9volt battery for locomotion. I ended up getting 2 RCX&#8217;s and a Scout but never really programed them. I always preferred to remotely control them.
 In my free time I build, design and fly model airplanes.. I build the small ones 40&#8221; and under. I also fly (my friends) giant 1/3rd scale gas models planes. I used to fly RC helicopters but there was too much B/S involved. Hence my decision to join the AirForce. By the way, ever since I started model aviation, in 7th grade, I have been using bits and pieces of math that I understood only because it was relevant to airplanes. Looking back, I realize I was actually doing very crude algebra, and using physics concepts. Only I didnt know I was doing algebra and physics! (Example, wing loading IS pressure, I just had no idea) Thats why I said I was never going to use that any way.[\i] 

Im just now getting seriously interested in computers. I dont have much money, (income is from painting (Im an artist too)) so my greatest limit is the machine I can afford. And it is a 100% stock Ibook G4 (no ram upgrade or anything!) I cant run any PC applications unless if I run Q emulator but I have yet to figure out how to down load it. I also dont want to use my XP key on it, saving that for when I get my macbook pro! Id like to play with Ubuntu Linux, but I am having ISP problems, so Id I have to either get it on CD or through a bittorrent file. Id rather avoid that bittorrent BS though. 


I tried getting Xcode, thought it was on my ibooks CD&#8217;s that came with it. I used Pacifist and couldnt find anything so I began looking over the C book I have.. its about 800 pages of pure C!
This is the first intro program..
I think I typed it right, I typed it like the picture. I put it into terminal and it reports an error.
The book mentions Borlands Turbo C  alot, not sure if I need that.  In the book, it shows diagrams for both MS-DOS and UNIX.

#include <stdio.h>

int main( void )
{
  printf("Hello World! \n" ) ;
  return 0 ;
}

This is the error:

Last login: Mon Dec  8 23:01:54 on ttyp2
Welcome to Darwin!
Joseph-C-iBook-G4:~ Joey$ #include <stdio.h>
Joseph-C-iBook-G4:~ Joey$ 
Joseph-C-iBook-G4:~ Joey$ int main( void )
-bash: syntax error near unexpected token `('
Joseph-C-iBook-G4:~ Joey$ {
>   printf("Hello World! \n" ) ;
-bash: syntax error near unexpected token `"Hello World! \n"'
Joseph-C-iBook-G4:~ Joey$   return 0 ;
-bash: return: can only `return' from a function or sourced script
Joseph-C-iBook-G4:~ Joey$ }






The guy who gave me the book, also gave me a micro computer to play with. It came with a compiler for C++. I installed it onto my desktop running XP. The desktop has been giving me straight crap since the ISP shut the internet off. It shuts off for no reason, but thats another problem. I havent been able to use the included compiler because the desktop is just so darned...well, PC&#8217;ish  Which is why I REALLY would rather learn how to make programs for the mac. Atleast the hardware is much much more stable.  My experience with RC planes comes into handy. I know to start small, every once in a while you will come a cross a guy trying to scratch build a 1/4th scale Edge 540 or a Turbine Jet helicopter...projects that take years of experience. I dont intend to start building full operating systems or anything, Id just like to acquire the tools I need to create a simple program, like a very simple + - / * calculator. If I could get that far, Id be satisfied (for a short period of time of course!) If I like it, I might pursue an education in it (on AF&#8217;s tab!) But id really hate to start a course and not like it, when I could be using it for something I need or enjoy..

So here are some questions I have came up with.. what is/are the compiler that I could use with Mac OS x 10.4? Is there any sample code I could look at? How do I get a program to run? (I know Windows uses .exe files, what do I need with a mac?) What is the difference between xcode and terminal? How powerful is terminal?

Im trying the K.I.S.S. method here _


----------



## ElDiabloConCaca (Dec 9, 2008)

I'll just jump straight to the questions, and say that's quite a path to take to enlightenment.    It's nice to hear someone take passion in something out of curiosity and because somewhere they learned what (I think) everyone learns at some point in their life: you're a lot damn smarter than you have been led to believe.  It just takes extraordinary circumstances to bring it out.

First, get XCode for your iBook.  Apple should be offering an XCode that is compatible with PowerPC processors, if the latest version isn't PowerPC-compatble (I think it is, but...).  You may need to sign up for a "Free" Developer's Account at http://developer.apple.com.  It's no trouble, and will allow you to download XCode.  Choose the version that is compatible with the version of OS X you're running.  Install it.  Along with a bunch of cool GUI tools (that you won't use at this moment), it also installs the necessary command-line compiler tools you need.

At any rate, let's see... you've gotten to the point where you type a program -- good.  Take that same program you have, and save it into a text file.  Save it as "program1.c" on your Desktop, in plain-text format (TextEdit has an option for this).

Then, launch the terminal, and type the following command verbatim:

```
cd ~/Desktop
```
...and press Enter.

Then, type:

```
gcc -o program1 program1.c
```
That "compiles" your "source code" (the code you typed) into an "executable binary" program -- basically, "gcc" (the compiler) interpreted your code, line-for-line, and turned it into something the computer can understand.  Now, you've got an executable program that will run on Mac OS X on the command line.

I'll break that last command down, word-by-word, because that's the syntax you'll use most of the time to compile simple C programs...
"gcc" -- the compiler.  In the terminal, whenever you type something at the command line, the first "word" you type is a program, or some binary built-in to the bash shell (that's the name of the Terminal program or environment you're using -- even the Terminal itself is just a program).  Everything after that, separated by spaces, are "arguments" to the program to tell it to do something.

The next two arguments go together:
"-o program1" -- means, "Make the output of this program (the gcc program) into the executable "program1" instead of "a.out" (which is what you get if you omit the "-o [executable name]").

After that, we have this:
"program1.c" -- the "input" to the gcc program (compiler).  Gotta tell the compiler which source code to compile, right?  

So, altogether we have a terminal "statement" that would look something like this in English:

```
gcc, please compile a program and make an executable called "program1" using my source code "program1.c"
```

Almost every UNIX program (something typed in the Terminal) can take "arguments" after the program name to modify and/or specify the way in which the program runs and what it does.

After you run through all that, to run your program, type this:

```
./program1
```
And, you should see "Hello, world" printed on your screen.

The "./" in front of the executable name tells the Terminal to "execute" the program located in the current directory ("./") named "program1."

If you wanted to execute a program named "program1" in your "Music" folder that is located in your home folder, you would type:

```
~/Music/program1
```
The "~" is shorthand for "home folder", and the rest is self-explanatory: "home folder" -slash- "Music folder" -slash- "program1".


----------



## Viro (Dec 10, 2008)

You're not supposed to type code _directly_ into the terminal. You're supposed to type it into a text editor and then save it as a .c file. So open up a text editor like TextEdit (make sure that it isn't saving in RichText), type in the code, save it into a file with .c as an extension (e.g. hello.c) and then drop down to the terminal.

Then follow ElDiablo's instructions on how to get that file compiled into a binary that you can run.


----------



## Satcomer (Dec 10, 2008)

ziomatrixacs said:


> I know, I should probably take a computer programing course for this but I want to do this on my own time/budget for a hobby.. I was thinking about getting into programing with straight C (not C++) and basically... where do  I start?



Check out O' Reilly Media books. They have really great tech books at this place for reasonable prices.


----------



## ziomatrixacs (Dec 10, 2008)

Thanks for all of the replies! The book I have simply said type this, didnt say what to do with it. Last night it actually hit me, that if I put it into a file, like most programs are then it might work. This happened when I ctrl clicked on an application and hit "show contents" then the "guts" of the application was there. Applications dont type or copy and paste to terminal, they can be executed by terminal. 

Nice to know that I can figure that little stuff out, but I wasnt sure on what to do after that.

Im about to try diablos instructions.. Ill let you guys know how it goes soon enough!


----------



## ziomatrixacs (Dec 10, 2008)

I think im doing something wrong. I used text edit and tried to save the program in text edit as plain text and it only shows 4 options:
Rich text format (RTF)
HTML
Word format
Word XML format

Then it said I had to name the program as program1.c.rtf

So I tried stickies, which has a plain text format. It made me save it as program1.c.txt

I went ahead and tried saving as both. I opened terminal and I think I changed the shell to zsh by accident. I was using Help viewer to get a better idea on Terminal. I tried to change it back but it looks like this now: 


ip-78-175:~ Joey$ 
ip-78-175:~ Joey$ cd ~/Desktop
ip-78-175:~/Desktop Joey$ gcc -o program1 program1.c.txt
-bash: gcc: command not found
ip-78-175:~/Desktop Joey$ cd ~/Desktop
ip-78-175:~/Desktop Joey$ gcc -o program1 program1.c.rtf
-bash: gcc: command not found
ip-78-175:~/Desktop Joey$ cd ~/Desktop
ip-78-175:~/Desktop Joey$ gcc -o program1 program1.c
-bash: gcc: command not found
ip-78-175:~/Desktop Joey$


----------



## ziomatrixacs (Dec 10, 2008)

Ok so I used a little common sense and went to prefreneces and there it was "save as plain text" so I hit that and there are alot more options.. ill give it another shot.


hmm, it let me save it as program1.c but still says command not found in terminal. Could it be the shell I am in?

Edit #3, I got the ADC membership from apple. I found xcode, I need version 2.2.1 and its dmg is 823.4mb which is quite large! And of course things expand when you open it..
I am going to have to be able to pause and unpause the download so I can download it over a few days. I currently use the libraries wifi for internet, I no longer have internet at home  Will safari or firefox let me pause and resume? Everytime I try it, it fails.


----------



## ziomatrixacs (Dec 11, 2008)

I had a buddy download xcode for me with his internet, I hope he got 2.2 and not the newer leopard/intel version. 

I played with terminal some more and got it back to look like it was before. In another book I picked up, it was kind enough to give mac, unix and windows instructions. It said go into Terminal and type:


> gcc -v


and if it says _Command not found._ then I need to get the libraries or something like that.. Im not sure what it means or where to get the libraries, but I did get the Command not found error..


----------



## ElDiabloConCaca (Dec 11, 2008)

"Command not found" means you don't have "gcc" (the c compiler program) installed on your machine.

If you install XCode, gcc is installed as well.  That's how you get gcc on your system.  You cannot compile any c programs until you install XCode, because you need to install XCode in order to have gcc on your system.

Your whole programming life depends on getting XCode installed on your system!    Without getting XCode on there, there really isn't much you can do in the way of programming, other than typing out a bunch of programs that you can't even compile and run until you get XCode on there...

Your number one concern should be getting XCode on your machine, first and foremost, before you go any farther.

While there are other ways of getting compilers onto your system, with your level of expertise, I would highly recommend sticking to the "preferred" way of doing things, which is XCode.

*XCode, XCode, XCode, XCode, XCode.*  Nothing more until XCode.  No typing programs, no Terminal navigation, no trying to compile programs, no nothing until XCode is on your system.


----------



## ziomatrixacs (Dec 11, 2008)

Xcode is on my friends computer right now, I need to meet up with him to get it onto my computer. I cant download it, the file is too large and I can only be online for an hour or two at a time at the library. I just hope he got the version I told him to get..

edit

I just got a hold of him and he got the latest version, 3.12 which wont work on OS 10.4. So we are going to get xcode 2.5, I should have it by saturday.


----------



## ziomatrixacs (Dec 11, 2008)

I was able to use his macbook with xcode 3.12 and I got the program to compile and run! Woot!


----------



## ziomatrixacs (Dec 14, 2008)

I got Xcode 2.5 now and it runs! I was able to compile a program with terminal and got it to run! Sweet.. I am still learning the menus though. 

Diablo, whats next?


----------



## ElDiabloConCaca (Dec 14, 2008)

Awesome!  Now you've had a taste of writing a program from scratch, compiling it, then running it and checking the output.

Now it's time to backtrack a bit and put on your learning cap.  I would recommend getting a good, beginner's book on C programming and starting from Chapter 1.

In actuality, though, it would be a good idea to get a grasp on some programming theory before jumping in and learning how to write code.  The difference between literals, variables, constants, and structure are all detrimental in being a "real" programmer.

Here's a great read on the history, philosophy and some pros and cons of the UNIX operating system (which is what Mac OS X is based on -- er, rather, Mac OS X *is* UNIX with a beautiful graphical interface on top of it):

http://www.faqs.org/docs/artu/

Here's a "jump right in and learn what the hell the code means" type of tutorial:

http://www.cprogramming.com/tutorial.html#ctutorial
(Start with "Intro to C" and move on from there)

But, if you don't know the different between an Integer and a Float or a Double, or a Char and a Char * (character pointer, or "String"), then I would recommend doing some research on primitive types and programming theory first.

Here's some tutorials for people who have never programmed before:

http://cplus.about.com/od/introductiontoprogramming/Never_Programmed_Before_Start_Here.htm

If you have any questions, or get overloaded (which is VERY easy to do when starting out), come on back and we'll help in any way possible.


----------



## ziomatrixacs (Dec 15, 2008)

I have 3 books on programing: C for dummies, Beginning programing for dummies and The indispensable guide to C.
The C for dummies kinda glazes over things and sugar coats it by saying "dont worry YOU dont have to do the math!" 
BP for dummies is OK, it tells you how to use BASIC and it wont work with my mac so its not real helpful.
The ind. guide to C is THICK, and is very very detailed and provides instructions for unix. Unfortunatly, the instructions arnt compatible. 
They tell you to compile a program by using this instruction:
$cc -o program1.c program1 
and it doesnt work like
gcc program1.c -o program1 

I already stumbled on to the Cprograming website, very helpful! 

I am dying from lack of internet though, the internet is very useful for this stuff but I can only access it at the library every few days.


----------

