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

smokeybear

macrumors newbie
Original poster
Jan 30, 2013
7
0
Hey guys I'm wondering if any of you can help me out. I'm using a Macbook Pro to start my school career in computer science.

Basically I'm beginning my course in learning the basics of C, like some of variables, expressions, keywords, etc. But tomorrow is my first lab, and I'm wanting to get a head start.

I wondering where do I begin to write this language on my Macbook. I've already looked around and couldn't find anything specific, but I've read around that I can use terminal to write it...or compile it? I'm not positive as to where to begin to write or test stuff out.

Any words of wisdom would be greatly appreciated.
Thanks
 

mfram

Contributor
Jan 23, 2010
1,304
341
San Diego, CA USA
The easiest way is to download XCode. One option is to use the IDE built-in to XCode to write C programs. The second is to download the "Command Line Utilities" (look in preferences) and use the Terminal to run gcc like any other Unix system.

There is a "Mac Programming" forum. You should take further programming questions there.
 

smokeybear

macrumors newbie
Original poster
Jan 30, 2013
7
0
Thanks, didn't gather all the information I wanted, but I sure did learn a lot from the advice given and with the help of Google!

About to enter lab; going to continue to use this thread to ask questions if y'all wouldn't mind sticking around!
 

mbecker16

macrumors newbie
Mar 29, 2012
5
0
So I'm a recent college grad and I think I have a good idea of what your professors are going to be looking for. You likely won't be using an IDE (integrated development environment) for you first courses. They will want you to learn the basics before that.

When you write a C program, you need two things. First, a text editor that you will write your code with, and second, a compiler that will take your code and output a program. The compiler reads in the C code and compiles it, which means that it first does some magic, that you will likely learn later, to transform it into an executable binary file (a program). That executable will be specific to your Operating system and CPU architecture (x86 or x64 for intel macs).

There is much debate over which the best text editor to use but there are two categories, terminal text editors, such as vim or emacs, and gui editors, such as smultron.

As far as the compiler goes, most universities use the gnu compiler known as gcc (g++ for c++).

There are several ways to get gcc on your mac but the easiest is to just install xcode, which installs gcc for you. As a side note, Apple has been using the llvm compiler a lot recently instead of gcc. That doesn't mean that you want to use llvm cause it's better, it's just a note.

Once you have the tools, it's pretty easy.

You create a new text file and give some name and the .c extension (Ex: something.c). Save the file
and then compile it like so

gcc something.c

gcc will output the program with the name a.out

if you type a.out, it will execute your program.

You can look for an example of a hello world application in C if you want to try it out.

That's the basics and enjoy.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,544
6,042
There are several ways to get gcc on your mac but the easiest is to just install xcode, which installs gcc for you. As a side note, Apple has been using the llvm compiler a lot recently instead of gcc. That doesn't mean that you want to use llvm cause it's better, it's just a note.

It's not quite as simple as "just install Xcode". After installing Xcode, you have to open the preferences, go to the downloads tab, and download the command line tools.

if you type a.out, it will execute your program.

I believe you have to actually type "./a.out", not just "a.out" for it to execute your code... if you'd like to execute it without the ./ (as you'd execute any other command line program) you'll need to put it in a special directory like /bin

Just because I brought the special directories up... I'd like to know, where should I put the executables for command line tools I write that I'd like to be able to easily access from any directory?
 

pitaya

macrumors member
Jun 17, 2012
34
0
Just because I brought the special directories up... I'd like to know, where should I put the executables for command line tools I write that I'd like to be able to easily access from any directory?

/bin isn't magically special. It's in the shell's command search path.

/usr/local/bin is a good choice. Is it already listed in $PATH? I don't know if it exists on OS X out of the box. You could add a file to /etc/paths.d/, or an entry to /etc/paths, if you want to use it. If you'd like to run these tools out of your home directory, you could add, e.g., ~/bin to your $PATH. If you're using bash, try putting this in ~/.bash_profile:

Code:
export PATH="$PATH:$HOME/bin"

You might want to check out man hier (which also includes a link to some apple documentation).
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.