# learning C



## nelson (Sep 23, 2002)

How can l see if a C compiler (gcc) is installed on my macox G4?

Thank-you


----------



## cbaron (Sep 23, 2002)

gcc isn't installed by default, u need to get the developper tools which installs it, or maybe get it from the GCC website.


----------



## nelson (Sep 25, 2002)

I've installed the "developer tools" Dec 2001 cd that came with the OS X


----------



## Ibson (Sep 25, 2002)

If you've installed the dev tools, you'll have gcc. Type "man cc" at the command line for more info.


----------



## nelson (Sep 26, 2002)

Thanks Ibson, I wasn't sure if it was installed.

Does anybody know where abouts do I write my simple "hello world" program in c or source code? (which editor should l use and where can l find it)
Thanks!


----------



## Ibson (Sep 26, 2002)

To make your first "Hello World" program, I suggest you start getting in the habit of using Project Builder. It has a great source editor, and is indispensable for resource management for large projects. When you're creating large Cocoa applications with hundreds of classes, nib files, and other resources, its really great. It also provides a nice front-end for gdb.

Find Project Builder in /Developer/Applications, then launch it. If the "New Project" panel isn't on your screen, choose File -> New Project. Scroll down to the bottom of the list, and select "Standard Tool". Click Next. Select a name and location for your project, then click Finish. 

To edit main.c, click the arrow next to the Source folder, and click on the file. And...wattaya know, but there's already a hello world application there!

To compile and run it, click on the toolbar button third from the left (the one with a hammer and computer screen). You can also press Command-R or choose "Build and Run" from the Build men. Project Builder compiles the project with gcc, then executes the program.

If you don't yet feel the need for Project Builder, you might like BBEdit. Of course, that isn't free and isn't so useful for larger projects, so I recommend you start off with PB.


----------



## nelson (Sep 28, 2002)

Thanks very much for the project builder tutorial was really helpful.

Would also like to work from the terminal with the vi editor, but having problems finding the right commands and typing the hash in at the tilde characters?

vi file.c                          /*type in at the shell*/
________________________________

       #include < stdio.h >      

       int main()
       {
       printf("hello world!\n")
       }
_________________________________                                              

 file.c                              /*compile it?*/

                                      /*load it?*/

                                      /*run it?*/

                                      /*find out where it went?*/

Appreciate any help on this.


----------



## buc99 (Sep 29, 2002)

After you open the vi window:

press "i" to insert text.
when you are done inserting press "ESC" button to exit insert mode. To save the file press  "shift - :" then press "w" followed by the name of the file if you did not supply that name when you first opened vi. If you did supply the name when opening vi, "w" will suffice. To exit press "shift-:" followed by "q" to quit. To force quit substitute "q!" for "q" and vi will quit without saving anything, unless you already saved it with "w". I like to combine these two as "shift-:" followed by "wq" then hit enter. It saves and exits in one fell swoop.

For more info on vi I suggest you research it on the net. Try google and enter "vi tutorial" and see how many hits come up. There are a lot of command combos to learn, but with a little practice it will become natural.

To compile I believe you would do the following:
cc -o foo.c

Then look for foo in that directory. You should be able to run it like so:

./foo

Good Luck.
SA


----------



## 96.9 (Oct 2, 2002)

You have to download  ( or install )  developer tools to get the gcc compiler if you are using OS X 10.1 operating system .

I am not sure about OS X 10.2 with it's setup and what comes with the CD Roms in the box .


----------



## 96.9 (Oct 2, 2002)

OK , you have to download developer tools for OS X 10.2 . The new developer tools only works on OS X 10.2 , there are older devloper tools for OS X 10.1 users and the upgrade to OS X 10.1.5 of course , available for download online . The basic OS X package comes disabled for easy home use and if you want you can use internet acess to download the software needed to turn OS X into a BSD computer from hell .

Mastering the C compiler is a great idea in computer studies , C++/Java is also a great idea . 

There are lots of great online howto and info websites for programming , I suggest you take some time checking them out and bookmarking them for further use  or recording the URL in a notebook that is handy when at a kiosk for internet useage .

Have fun


----------



## nelson (Oct 7, 2002)

Compiling in the terminal l am getting the following read out,

warning: numeric constant contains digits beyond the radix

In function `main':
numeric constant contains digits beyond the radix

_______________________

if (weight >= 20 && weight <= 50)
{ amount = 090;}

_______________________

what is wrong in the above code?


Thank you


----------



## lurk (Oct 7, 2002)

The problem is that you cannot just stick extra zeros on the front of a number because it changes the base


```
10 = 1+1+1+1+1+1+1+1+1+1
 010 = 1+1+1+1+1+1+1+1
0x10 = 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
```

0 markes a constant as being in Octal (base 8) and 0x marks it as being in hexadecimal (base 16).  In your particulat case 090 is not a number because there is no digit for 9 in Octal numbers, then you would have to us 11 as 9. The radix is the base -1 and basically represents the largest digit you can use be it 1, 7,9, or F for some common ones.

Clear as mud huh...

The short answer don't stick zeros in front of integers unless you know what you are doing 

-Eric

P.S. For floats both .2 and 0.2 ar O.K.  but I cannot say what 00.2 or 0x0.3 would do.  My guess is that they would break , I mean god forbid there could be any consistancy in a language...


----------

