PDA

View Full Version : Learning C Language on my Mac...




QueenZ
Jan 22, 2009, 11:26 AM
Hey there! I've been learning C++ on my Mac with XCode and it worked fine. Now i would like to learn C but i don't think that XCode supports it.. :( All i could find was C++

So weather please suggest me some C compilers for mac or maybe i just don't know how to do C coding in XCode..

#include < stdio.h>

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



Menge
Jan 22, 2009, 11:35 AM
Xcode support C, C++, Java AND Objective-C natively. Go with it.

toddburch
Jan 22, 2009, 11:36 AM
It supports C. When creating a new project, in the same dialog where you select a C++ command line tool, select "standard tool" instead for C.

QueenZ
Jan 22, 2009, 11:37 AM
I love Xcode but which template should i choose so that the code that i posted earlier would work? As far as i know Command Line Utility > C++ Tool doesn't work.. :(

It supports C. When creating a new project, in the same dialog where you select a C++ command line tool, select "standard tool" instead for C.

OK, did just like you said but.. still getting errors.. :(

http://farm4.static.flickr.com/3366/3218438322_7e20bd1536_o.png

OK, i get this error that return type of main is not int... that is really weird since i followed the tutorial which goes like this..

Your First C Program
Let's be polite and start by saluting the world! Type the following program into your favorite editor:

#include <stdio.h>

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

It's clearly void main not int main... do you guys think that the tutorial could be wrong or something..?

cherry su
Jan 22, 2009, 12:04 PM
make a new main.c file in your home directory:

#include < stdio.h>

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

and open the terminal, type gcc main.c -o test
you will get an executable called test
then in the terminal type ./test
voilà!

QueenZ
Jan 22, 2009, 12:11 PM
I still don't know why i get that warning.. :(

toddburch
Jan 22, 2009, 12:14 PM
1) Get rid of the space inside here: < stdio.h>

2) write this:

int main(void) { .... not void main(void) {

The compiler is telling you "the return type is not int, and it should be"

toddburch
Jan 22, 2009, 12:20 PM
... do you guys think that the tutorial could be wrong or something..?

The tutorial is most likely old. It used to be standard practice for a program to decide to ignore returning anything to the calling process to indicate its success or failure.

gnasher729
Jan 22, 2009, 12:26 PM
OK, i get this error that return type of main is not int... that is really weird since i followed the tutorial which goes like this..



It's clearly void main not int main... do you guys think that the tutorial could be wrong or something..?

It wouldn't be written by Helmut Schildt, who got the nickname "Bullschildt" for his lets say innovative approach to C programming?

If the tutorial says "void main (void)" then it is badly wrong. Especially for a command line tool, where the return value of a tool will often be used by shell scripts to determine whether the tool ran into any problems.

toddburch
Jan 22, 2009, 12:36 PM
It wouldn't be written by Helmut Schildt, who got the nickname "Bullschildt" for his lets say innovative approach to C programming?


lol!!
I have a couple of his books. It's "Herb".

keekl
Jan 22, 2009, 12:42 PM
Wish I was younger to learn this...

Patillac
Jan 22, 2009, 02:30 PM
I was gonna ask a similar question as to how exactly one should use XCode to learn C. I'm SUPER new at this programming stuff, and even using XCode has been a task in and of itself :confused: Nonetheless, I'll continue to follow along, but wanted to say thanks & kudos to the MR guru's who take the time to give advice and lend a helping hand
Much appreciated!

cherry su
Jan 22, 2009, 04:55 PM
I was gonna ask a similar question as to how exactly one should use XCode to learn C. I'm SUPER new at this programming stuff, and even using XCode has been a task in and of itself :confused: Nonetheless, I'll continue to follow along, but wanted to say thanks & kudos to the MR guru's who take the time to give advice and lend a helping hand
Much appreciated!

Xcode is the IDE (integrated development environment)…it's basically a replacement for terminal development

since you are a beginner, you should use a terminal and a text editor. the terminal can be found in your Utilities folder and the text editor can be found in your Applications folder (use TextEdit and make sure it's in "plain text" mode). use TextEdit to write your programs and use the terminal to compile your programs

also, you should buy the "C" book written by kernighan and ritchie

Patillac
Jan 22, 2009, 06:38 PM
Xcode is the IDE (integrated development environment)…it's basically a replacement for terminal development

since you are a beginner, you should use a terminal and a text editor. the terminal can be found in your Utilities folder and the text editor can be found in your Applications folder (use TextEdit and make sure it's in "plain text" mode). use TextEdit to write your programs and use the terminal to compile your programs

also, you should buy the "C" book written by kernighan and ritchie

Thanks for the advice!! Maybe over time I'll get better at this stuff and find a need for XCode

I picked up the Kernighan & Ritchie book for a reasonable price at a local used bookstore. I remember seeing it recommended here on MR, so I grabbed it. I have always wanted to learn how to program, but merely for hobby purposes and figured I'd get the book and give it a shot.

I was thinking I wasted the $$ since there are SOOO many online tutorials, but I'm hoping that it will prove useful once I get the whole compiler thing figured out :o

FlashTheInfidel
Jan 23, 2009, 05:22 AM
Xcode is the IDE (integrated development environment)…it's basically a replacement for terminal development

since you are a beginner, you should use a terminal and a text editor. the terminal can be found in your Utilities folder and the text editor can be found in your Applications folder (use TextEdit and make sure it's in "plain text" mode). use TextEdit to write your programs and use the terminal to compile your programs

also, you should buy the "C" book written by kernighan and ritchie

I feel obliged to point out that this is only one point of view concerning IDEs and learning programming.

Using TextEdit and a terminal will not give you many of the advantages that XCode's editor has - syntax hi-lighting, code completion, much easier access to debugging facilities and clearer hi-lighting of errors to name but a few. You'll have to manually compile and link all your programs (unless you go through the headache of learning to write makefiles right from the start) when you could be spending your time better actually learning and enjoying the process of programming. When you're starting out in something like this you need as much help as you can get so why make it more difficult right at the beginning? There is plenty of time to learn the nuts and bolts of compiling and linking once you have the basics down. Besides, for larger projects, working entirely in a non-programming text editor like this is pure masochism - you might as well learn the tools you'll most likely use later now.

Also note that if you're only ever going to write hobby projects for MacOS (or any well-supported platform) then you never actually need to learn this other stuff as it can all be achieved within XCode.

IMO the suggested approach just makes things difficult and dull when learning C is hard enough already.

BTW Your original problem would have been harder to solve with the TextEdit/terminal combination suggested. The lesson to learn there is to notice the difference between warnings and errors, I think. Thanks to Xcode's big red hi-lighting you can spot the major problem in literally seconds even though there's only a single character wrong.

Anyway, I don't want to confuse you as you start out and the advice given above is reasonable (just a different point of view). It's just that programming is one of those fields where there is definitely more than one way to crack a nut.

mdeh
Jan 23, 2009, 06:31 AM
I feel obliged to point out that this is only one point of view concerning IDEs and learning programming.

Using TextEdit and a terminal will not give you many of the advantages that XCode's editor has - ................... - you might as well learn the tools you'll most likely use later now.............
BTW Your original problem would have been harder to solve with the TextEdit/terminal combination suggested.


I have to say I agree completely. Xcode may be a little intimidating at first, but it will not take long for you to see it's advantages. I used Xcode to learn C and never regretted it.


In addition, I suggest you get 2 books and join 1 list.

1)The C Programming Language (sometimes referred to as K&R, or the white bible, or K&R2 for the second edition) is a well-known computer science book written by Brian Kernighan and Dennis Ritchie


2)The C Answer Book (Paperback)
by Clovis L. Tondo (Author), Scott E. Gimpel (Author).. a little dated in it's answers, but still good to have.

3) Join "comp.lang.c" ...where you will find hundreds of brilliant C programmers who will be more than willing to help you.

Personally, I do not think taking a short cut to learning a language will serve you well in the long run. :(

LtRammstein
Jan 23, 2009, 11:57 AM
I agree with using Terminal and a text editor. I would not use TextEdit.app, but try working with VI, VIM, EMACS, etc. This way you can gain knowledge on using those, and it can improve your knowledge of *nix commands.

lee1210
Jan 23, 2009, 12:47 PM
This is another age-old argument that will not be won here. I will state as briefly as possible what I have every time this comes up:
If you ever plan to develop on any platform other than OS X, in any language that is not supported by XCode, use the terminal. If you program on windows, there is no XCode, so you have to start again. If you program on Linux, there is no XCode, so you have to start again. If you program on Solaris, there is no XCode, so you have to start again. Your knowledge of the language carries over, but the tools you are so used to having available are all gone, or at least packaged in a way that you do not know how to use them readily.

If you only plan to program on OS X, ever, then XCode is fine. Otherwise a knowledge of the command line and its tools are a must.

As for what editor to use, i prefer vim at the terminal, but there are plenty of fancy programmers' text editors available on OS X. Again, i would pick one that is crossplatform to avoid losing one of your tools if you move to a different platform.

-Lee

Patillac
Jan 23, 2009, 01:29 PM
I never realized that learning the language was only HALF the battle. It seems learning the "supporting cast" of tools is equally as important. I don't intent to go cross-platform and as of last night seemed to get the hang of XCode writing C programs. 100 mistakes and "doh's" sure make 1 success (even as simple as Hello World) seem like I conquered everest!! :D

I'm curious as to what you guys saw yourself doing when you were learning the basics and how it has evolved. Do people actually learn this for "hobby" or am I just weird? I'm extremely fascinated with Macs and the incredible detail that goes into programming and lately i've found myself eager to get off work just to get home and grab the K&R book and get on the computer.

lee1210
Jan 23, 2009, 02:42 PM
I never realized that learning the language was only HALF the battle. It seems learning the "supporting cast" of tools is equally as important. I don't intent to go cross-platform and as of last night seemed to get the hang of XCode writing C programs. 100 mistakes and "doh's" sure make 1 success (even as simple as Hello World) seem like I conquered everest!! :D

I'm curious as to what you guys saw yourself doing when you were learning the basics and how it has evolved. Do people actually learn this for "hobby" or am I just weird? I'm extremely fascinated with Macs and the incredible detail that goes into programming and lately i've found myself eager to get off work just to get home and grab the K&R book and get on the computer.

I guess i'd put it this way:
When you're constructing a building, what is more important: the blueprint, or the tools you use to do the actual construction? One way or the other, if you don't know how to draw a blueprint you aren't getting anything built, and without tools or knowing how to use them you aren't getting anything built. In my opinion if you only learn a specific, specialized brand of nailgun (XCode) you will be more limited than if you start with a hammer and work your way up.

-Lee

FlashTheInfidel
Jan 24, 2009, 06:22 AM
I guess i'd put it this way:
When you're constructing a building, what is more important: the blueprint, or the tools you use to do the actual construction? One way or the other, if you don't know how to draw a blueprint you aren't getting anything built, and without tools or knowing how to use them you aren't getting anything built. In my opinion if you only learn a specific, specialized brand of nailgun (XCode) you will be more limited than if you start with a hammer and work your way up.

-Lee

Real world analogies never really translate to computers very well IME. We've all heard the one with Windows PCs and cars and while it's funny it isn't really very useful.

I've never been to architect school, but I doubt they teach you to use a hammer and I'm certain they don't teach you about blueprints at the same time. And in this case you _can_ build a house with just the blueprint (C code) and asking a builder to make it for you (XCode).

My point is not that the terminal/editor combination isn't something worth learning it's just that IDEs like XCode give you the opportunity to learn one thing at a time (C programming not hammers:P). Once you're getting the hang of C or if you want to work on a platform without an IDE then it might be worth learning terminal compilation now that you're at a stage that you're comfortable with programming concepts. The cross-platform argument only goes so far, since all the platforms mentioned above have competent IDEs available for them that all do a lot of the hard work for you and all work in very similar fashion. They're also designed to improve your productivity compared to terminal/editor coding and in my experience this certainly helps.

Anyway, you're correct when you say that this argument can't be won - I just figured I'd present another point of view.


I'm curious as to what you guys saw yourself doing when you were learning the basics and how it has evolved.

I was a hobbyist for years (like every pro, I guess). I started with Atari 800XL BASIC believe it or not. I only came to C comparatively late as a hobbyist (I work mostly in C++ these days). While I knew the basics of terminal compilation etc. (mostly from Java programming I'd had to do) I cut my teeth using CodeWarrior IIRC.

I never needed to use a command line and text editor until I started my current job which is often working with mobile devices (IDE? Sometimes mobile platforms don't even have a proper compiler...). I found learning terminal compilation tedious enough then, but could concentrate on this single aspect of the process since I knew my coding was sound. However, even in this job, 90% of the time I work in Visual Studio or XCode (thank you iPhone) and only when I have the program working there do I need to recompile for other platforms. I hadn't used XCode since it was called ProjectBuilder (and not much at that) before the iPhone turned up and I found it very easy to pick up after using Visual Studio.

Programming is a great hobby and I know if I ever changed career I'd still be hacking away in my spare time in some shape or form. Good luck with the learning :)

waldroje
Jan 24, 2009, 07:12 PM
I picked up on this thread after trying to write my first c code today. I am using the H&R book, and had one quick question. I am writing the code in text editor and then using the command line in Terminal. The book simply uses syntax such as main() {....}, which does not seem to compile and results in errors, while if I use xcode it seemed to put in something like this, int main (int argc, const char * argv[]) {...} . Where can I read about the syntax which is specific to this compiler?

eddietr
Jan 25, 2009, 09:10 PM
I picked up on this thread after trying to write my first c code today. I am using the H&R book, and had one quick question. I am writing the code in text editor and then using the command line in Terminal. The book simply uses syntax such as main() {....}, which does not seem to compile and results in errors, while if I use xcode it seemed to put in something like this, int main (int argc, const char * argv[]) {...} . Where can I read about the syntax which is specific to this compiler?

It really doesn't have anything to do with the compiler. It's just that you don't necessarily have to declare the parameters which main accepts (argc and argv). Unless of course you need to use argc and argv within your main(). (And even if you aren't going to use them immediately, it's good practice to declare the parameters.)

Which compiler are you using from the command line? I assume it is gcc, which is the same compiler used by XCode.

If you post your code and the errors you are getting, maybe I or someone else can point you in the right direction.

Sander
Jan 26, 2009, 03:56 AM
I'm with Lee on this, although I agree it's an argument which will probably never be won.

My usual argument is that it's easier to forget about the IDE for the first few projects. For a long time, your programs will fit in a single file, and if you're already have a favorite plain text editor and know what Terminal is, I would argue that "gcc -Wall -o myprogram myprogram.c" is not too hard to learn.

The IDE, in my view, makes things easier once your projects start to grow, your programs get split over multiple files, you want to use the debugger, etc.

There's also the fact that the "gcc" command line is exactly the same as when I first typed it 15 years ago, whereas IDEs have changed drastically in the mean time.

But then again, I grew up typing and not clicking. There really are people for whom it is easier to open up the IDE, find "new project", browse through the list of project types, select "empty console project" in the "C/C++ projects" subtree, etc.

In the end, it doesn't really matter. It's far more important to get this silly "void main" thing erased from our collective memories :-)

hoangluong
Jan 27, 2009, 03:37 AM
Hi, does anyone know where to get the most essential commands often used in Terminal when one is learning C? Thanks a lot!

AUFan
Jan 27, 2009, 08:39 AM
Hi, does anyone know where to get the most essential commands often used in Terminal when one is learning C? Thanks a lot!

Google "Unix terminal commands" , "gcc commands" , or "OS X terminal commands" , or "Mac terminal commands" and you should come up with a litany of websites that cover all the UNIX commands in terminal. I found a pretty good document searching along those lines whose title escapes me, (it's on the home comp), that did a decent job of explaining the basic commands needed to compile C programs.

subsonix
Jan 27, 2009, 01:49 PM
hoangluong, type man gcc in terminal. The man command gives you a manual for gcc in this case, but all standard c and unix commands have man pages.

AUFan
Jan 28, 2009, 12:55 AM
hoangluong, type man gcc in terminal. The man command gives you a manual for gcc in this case, but all standard c and unix commands have man pages.

That is another way to do it, but all man does is give you the list of commands and a little explanation. I've found you usually get better info if you google it and better explanations/examples which are useful when first starting out.

hoangluong
Jan 28, 2009, 04:11 AM
Thanks to AUfan and subsonic! I found your suggestions very useful. Is this link below basically the same thing?... I'm so new to programming, so I'm trying to start from the very group up...

http://www.ss64.com/osx/