Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

w...b

macrumors regular
Original poster
Jul 14, 2008
187
0
i'm very new at this and im starting scratch. im just searching online on how to start C - programming.

thanks
 

cromwell64

macrumors regular
Jun 30, 2008
160
0

wizard

macrumors 68040
May 29, 2003
3,854
571
Xcode. It is free!

Hi there, it has already been mentioned but part of the answer is Xcode which comes with Mac OS. You should install it if it isn't already.

Some people are not happy with XCodes editor so you can always try others. Free examples are MacVim and Eclipse, though it doesn't take much effort to find many more.

Some people like the tools and stuff supplied by MacPorts, I DO NOT but include it for completeness. It could be argued though that learning GNU tools is a good thing, frankly though you would be better off installing Linux in a virtual machine.

Dave
 

allmIne

macrumors 6502a
Sep 17, 2008
771
0
United Kingdom
As above, xcode!

If you load up xcode from the OS X disk (it probably wasn't installed by default) then go to FILE > NEW PROJECT then scroll down to Command Line Utility, select that then STANDARD TOOL.

That'll set you up with everything you need to edit, compile and run command line programs.
 

ChrisA

macrumors G5
Jan 5, 2006
12,919
2,173
Redondo Beach, California
It could be argued though that learning GNU tools is a good thing, frankly though you would be better off installing Linux in a virtual machine.

As soon as you open up a terminal window Mac OS and Linux are the same thing. Anything that runs under Linux will also run under Mac OS X. (OK maybe "anything" is to strong but from the point of view of a begiinig C programmer mac OS X and Linus are identical.)

How to decide between xcode and simply using the command line? I'd say that at the very beginning when you are doing very simple projects the command line is very simpler. Also later you have to decide if you need your software to be portable and run of many different platforms. xcode is "Mac Only". Use xcode for programs that are not-triveal student type things that are going to run only under mac OS X.
 

w...b

macrumors regular
Original poster
Jul 14, 2008
187
0
i cant find the Xcode on my new MBP. Its running the latest leopard.
 

hoangluong

macrumors member
Jan 12, 2009
37
0
Sydney, Australia
As above, xcode!

If you load up xcode from the OS X disk (it probably wasn't installed by default) then go to FILE > NEW PROJECT then scroll down to Command Line Utility, select that then STANDARD TOOL.

That'll set you up with everything you need to edit, compile and run command line programs.

Hi, thanks for your help! I'm very new to programming and I'm teaching myself C on the Mac. I've done as you said but I just cannot click the button "Compile", etc... on the menu. They all are blurred and unclickable.

When I switch to a PC, I use Miracle C which has the Compile, Build and Run buttons. I just write a simple C program, save it with a .c extension, then click on each of the buttons above. So my question is, do I have a similar facility on the Mac, i.e. on the Xcode or Codewarrior??? I'm using CodeWarrior for C learning on the Mac but just like Xcode all the buttons I need are blurred and unclickable.

Please help!!! Thanks a lot
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Xcode (and presumably Code Warrior) are designed to work on projects rather than individual files. You'll need to create a new project (choose 'Standard Tool' as the type for now) in order to be able to compile it.
 

BravoBug

macrumors newbie
Dec 8, 2008
26
0
Oregon, USA
If you really want to avoid using xcode projects when you first start out, just compile your code directly with gcc, which will be installed when you install the dev tools.

ie, put this in a text file called main.c:

Code:
#include <stdio.h>

int main()
{
	printf("Hello, world!");
}

now launch terminal, and compile it:
Code:
gcc "main.c" -o "MyProgram"

now to launch/test:

Code:
./MyProgram

Should print Hello, world! to the terminal before quitting.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
A few minor nitpicks:

The quotes around the file names are not necessary (they come in handy when there are spaces in your file names though), and the main() function should return a value. If you compile with

Code:
gcc -Wall -o hello hello.c

which is a good idea (-Wall turns on all warnings), gcc would actually tell you that "control reaches end of non-void function". Simply add a

Code:
return 0;

at the end of your main(). Also, add a newline character ("\n") to the end of your greeting; otherwise the next command prompt will be printed directly next to the text when you run it from a terminal.

Have fun programming!
 

hoangluong

macrumors member
Jan 12, 2009
37
0
Sydney, Australia
Thank you. I've tried the suggestions you gave, but for some reason unknown to me, it didn't work. I've tried the following and somehow it works.

The file:
#include<stdio.h>
int main(void)
{
printf("Hello World!!!");
return 0;
}

I had to drop this saved file onto Terminal prompt after entering gcc with a space. And the output looks like this:

hoang-luong-luongs-macbook-pro:~ hoangluongluong$ gcc /Users/hoangluongluong/Desktop/IntroC/Hello.c
hoang-luong-luongs-macbook-pro:~ hoangluongluong$ ~/a.out
Hello World!!!hoang-luong-luongs-macbook-pro:~ hoangluongluong$

Can you help explain why? Also, does it mean that I can't have all-in-one device that edits, compiles, links and runs the program all within one compiler?

I've created a New Project in Xcode, chose Standard Tools, then Open a new file and chose C file. I wrote a very simple program in it, clicked Build and Run on the menu. It said "Build failed (1 error). I clicked on this warning and saw "xCommand/Developer/usr/bin/gcc-4.0 failed with exit code 1". I've attempted to click on Compile but it is all blurred.


Thanks a lot for your time and help!
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Did you add this new file to the project, or just 'open' it?

Unless you add the file the project, it won't compile.
 

hoangluong

macrumors member
Jan 12, 2009
37
0
Sydney, Australia
Did you add this new file to the project, or just 'open' it?

Unless you add the file the project, it won't compile.

Hi, this is how I did it and it didn't work

- Xcode->New Project->Command Line Utility->Standard Tools->Choose a name & save it.

- Within the window for this project just initiated, File->New File->C and C++ -> C File-> Choose a name ending with .c for the program I wanna write. This file is sure to be added to the project I created.

- Once in the window for the newly created file, click on the filename ending with .c. Then I clear all the stuffs on the window to make way for the simple program I wrote, then saved it and click "Build and Run" on the "Build" menu. At this point it returned the error as I said in the previous post.

Can you tell me how to run a program from within Xcode? I mean being able not only to write it but also compile, link and run it within Xcode without resorting to another compiler?

In a PC, I use a compiler called Miracle C and I can do everything from writing to building, compiling, linking and running it within this compiler. It even reports any errors if any. My question is whether I can have such a thing with Xcode?

I'm so new to programming, so please excuse me for these silly questions.

Thanks a lot
 

hajime

macrumors 604
Jul 23, 2007
7,924
1,312
In my opinion, the simplest way is to open an xterm, use vi as the editor and then gcc to compile the programs.
 

jgrogan

macrumors newbie
Jan 14, 2009
3
0
Hi All,

Just to follow on from this thread with a question, I've recently moved over to a Mac having used PC's for years. Although all my development work is in Visual Studio (in parallels), I'd like to start exploring projects on OSX.

Can anyone recommend any good books or other sources to help me get started?

Many thanks,
John.
 

dannomac

macrumors member
Mar 11, 2008
95
0
Saskatoon, SK
Can anyone recommend any good books or other sources to help me get started?

If you're looking for how to write programs specifically for OS X, then I recommend this: http://oreilly.com/catalog/9780596003012/. It's a little dated, but it's a good foundation book for OS X programming for people who already know C or C++. After that, Apple's documentation is extensive. I have several binders full of Apple docs printed, because I don't like reading on the screen.

I still don't know Objective C 2.0, but I plan on learning soon. I've been in school for the last bit, using UNIX programming exclusively, so Mac OS has been ignored except for GCC and make :)

Dan
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Also, have a look at iTunes U / engineering. There is a complete university course in computer science consisting of 44 one hour long video lectures, most of it is based on C, and it´s free too.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.