PDA

View Full Version : C compiler




anmldr
Sep 3, 2008, 12:01 AM
Newbie to Mac, ObjC, Xcode, etc. Background more in Palm and other PDA programming using Visual Basic like languages.

I would like to simply write a Hello World app in C and compile it. I am not sure what tools are available.

I do have Smultron as a text editor. If I write the code in Smultron and save it as hello.c then I use Terminal and enter gcc -o Hello hello.c I get an error that is "return type of ‘main’ is not ‘int’"

#include <stdio.h>

void main()
{
printf("Hello, World\n");
}


I need suggestions for if the route that I am using is best to get a start in C on an Intel Mac and any other guidance that you offer.

Thanks,
Linda



lee1210
Sep 3, 2008, 12:31 AM
you are on the right track. Just switch
void main()
To
int main(int argc, char *argv[])

You are well on your way to recovery from BASIC. Good luck!

-Lee

kpua
Sep 3, 2008, 01:15 AM
FYI, the argc and argv arguments are not needed. It is perfectly fine to leave them off. It's a common misconception that they are required.

(A little known fact is that even when you specify both argc and argv, you're still leaving one argument off. There's a "char **env" argument that contains a list of all environment variables.)

But, the return type must be int in C, because the parent process uses it to get the exit status.

Cromulent
Sep 3, 2008, 06:02 AM
FYI, the argc and argv arguments are not needed. It is perfectly fine to leave them off. It's a common misconception that they are required.

(A little known fact is that even when you specify both argc and argv, you're still leaving one argument off. There's a "char **env" argument that contains a list of all environment variables.)

But, the return type must be int in C, because the parent process uses it to get the exit status.

But if you do leave argc and argv off, you must put void in its place otherwise you stray into implementation specific territory.

mobilehaathi
Sep 3, 2008, 07:03 AM
Might as well

return 0;

at the end for good form.

anmldr
Sep 3, 2008, 09:02 AM
But are Smultron and Terminal the way to go to get my feet wet in learning C? Or are there better options? I just need to have a little background in C, then move onto C++ for a short while before REALLY diving into ObjC and iPhone programming. Every book and article or other source for iPhone development start out saying that you need a background in C and an object oriented programming environment like C++. I did take a college course years ago in C++.

Pet peeve of mine :-) Why doesn't someone write a complete beginner book on Xcode, ObjC and Interface Builder that introduces the concepts needed? Instead, all of the books, tutorials, videos, etc. assume that you have a background in other languages.

I was able to pick up other programming languages for the Palm and Windows Mobile environments without first having a background in coding for the desktop environment. I would like to port some of my other PDA apps to the iPhone....knowing they will be quite different on the iPhone. But, having to learn 2 other languages BEFORE learning ObjC is a pain. Diving into ObjC without any background in C/C++ would be preferable to me.

Oh well. I am off to do my homework.

If you have a suggestion for a book that is now available that would help me as a beginner to ObjC without first leaning C and brushing up on C++, I would appreciate it.

Linda

MrStevieP
Sep 3, 2008, 09:38 AM
FYI, the argc and argv arguments are not needed. It is perfectly fine to leave them off. It's a common misconception that they are required.

(A little known fact is that even when you specify both argc and argv, you're still leaving one argument off. There's a "char **env" argument that contains a list of all environment variables.)

But, the return type must be int in C, because the parent process uses it to get the exit status.

If you check the wikipedia for Main Function, you'll also find that on Mac OS X you can have a fourth parameter for OS related stuff.

mobilehaathi
Sep 3, 2008, 09:46 AM
But are Smultron and Terminal the way to go to get my feet wet in learning C? Or are there better options? I just need to have a little background in C, then move onto C++ for a short while before REALLY diving into ObjC and iPhone programming.


Everyone will have their own opinion on this. I learned C++ on the command line with emacs, gcc, and make, and I wouldn't have it any other way. I would stick to what you're doing, since you'll probably pick up a good understanding of a lot of what goes on underneath an IDE anyway. Eventually, I guess, you'll pick up Xcode and do your iPhone programming in that. I haven't programmed for the iPhone (yet!), but I get the impression things are nicely streamlined within Xcode (and IB?) to allow for quick work on some of the more tedious bits.

Best of luck!

iSee
Sep 3, 2008, 10:05 AM
Pet peeve of mine :-) Why doesn't someone write a complete beginner book on Xcode, ObjC and Interface Builder that introduces the concepts needed? Instead, all of the books, tutorials, videos, etc. assume that you have a background in other languages.

They have! Unfortunately, it hasn't yet been updated in a while. Still, the first ~ half of the book is the beginners guide for the Objective-C language you are looking for that does not assume prior experience with other languages.

It does not cover Objective-C 2.0 features, the massive changes to Interface Builder, or the changes to Xcode. But you will get a good understanding of Objective-C and Cocoa memory management techniques (since you can't use garbage collection for iPhone development, you'll need to know this).

There's a new version of the book in the works, but I'm not sure when it's coming out: http://www.amazon.com/Programming-Objective-C-Developers-Library-Stephen/dp/0672325861/ref=sr_1_2?ie=UTF8&s=books&qid=1220453293&sr=1-2

ChrisA
Sep 3, 2008, 10:11 AM
But are Smultron and Terminal the way to go to get my feet wet in learning C? Or are there better options? I just need to have a little background in C, then move onto C++ for a short while before REALLY diving into ObjC and iPhone programming.

Yes. Start with the terminal and a text editor. Write some simple, non-graphic text oriented stuff. Then move on to using Apple's xcode. But write now you don't need to try to learn both xcode and C at the same time.

I typicaly have 4 to 6 terminal windows and a few editor sessions up at once. I pretty much leave them up for days at a time. The code I write has to be portable to other *nix OSes.

lee1210
Sep 3, 2008, 10:14 AM
I definitely think that a text editor for writing code, gcc for compiling and linking code, and gdb for debugging are the way to go starting out. You will get a better idea of what's really going on, and won't have everything hidden away from you by the IDE. For C there's very few times that code completion actually comes into play (i guess for structure elements... i don't know if XCode does this for you or not), and to me that's one of the best parts of using an editor in an IDE.

I would say that C is a fine place to start... i don't know that you need to learn OOP concepts before starting Objective-C, though. I think it would be fine to learn OOP with the object model provided by objective-C. You'll miss out on some things like multiple inheritance, but I think you'll get by OK without that. If you move to a language that supports multiple inheritance after learning OOP with Objective-C, it shouldn't be hard to pick up.

-Lee

anmldr
Sep 3, 2008, 11:41 AM
They have! Unfortunately, it hasn't yet been updated in a while. Still, the first ~ half of the book is the beginners guide for the Objective-C language you are looking for that does not assume prior experience with other languages.

There's a new version of the book in the works, but I'm not sure when it's coming out: http://www.amazon.com/Programming-Objective-C-Developers-Library-Stephen/dp/0672325861/ref=sr_1_2?ie=UTF8&s=books&qid=1220453293&sr=1-2

Thank you! I had already ordered the next edition when it is released...not knowing anything about the book really except for it's title and author. Now that you recommended it, I went ahead and ordered a used book and will get working with it until the new edition is released.

There are LOTS of new iPhone developer books coming out soon. I guess the holdup is the NDA. I have many of these preordered as well.

I am a recent Mac convert. I do love my Macbook and am enjoying learning to program for it.

Actually, I am an old and new Mac user. I owned the second Mac made...the 512K "portable" computer. I still have it and it probably still works. I need to find a museum somewhere probably to donate it to :-)

Linda

anmldr
Sep 3, 2008, 11:18 PM
Have been coding most of the day using Smultron and Terminal. Everything seems to be working great.

Thanks for your help.
Linda

TheReef
Sep 4, 2008, 12:33 AM
For you or anyone else browsing this thread, for coding C in XCode here's a tutorial I found useful starting out.

http://masters-of-the-void.com/what-you-need.htm