How to: Write and compile C programs on Mac OS X

UPDATE: There is a new post about compiling C programs entirely within Xcode using Mac OS X 10.7+. Read it here.

During a programming class back in 2006, I had to find a way to write and compile C programs on my iBook G4. This was one of the ways I found to do that job.


  • Apple Developer Tools (available on OS X Install DVD)
  • A text editor (I use TextWrangler)


  1. Install Apple Developer Tools on the Mac OS X Install DVD by inserting Disk 1 and choosing Developer Tools.
  2. Write a program with a text editor and save it with the extension .c
  3. Open Terminal by navigating to Applications > Utilities > Terminal
  4. Type gcc with a space afterwards
  5. Drag the file with the .c extension to the Terminal window to place the location of the file.
  6. Press Enter.
  7. This will generate a file called a.out inside your user folder.

  8. Type ~/a.out
  9. Press Enter.
  10. Watch your program come alive!

[tags]programming, Mac OS X, Terminal[/tags]

{ 17 comments… add one }

  • mvdhoef November 21, 2007, 12:33 pm

    instead of resorting to the default a.out you can use gcc the way it was meant to be used!!!
    gcc -o -Wall name file.c
    where name is the name of the program gcc will create,
    file.c is the file and perhaps extension of the code you have created.

    -o is to open new/rewrite in this case name (*if name already existed, it would overwrite without a second thought)
    -Wall is another option which tells gcc to show all errors it encounters during compiling. (*this is optional)

  • mad angel November 14, 2008, 8:03 am

    i used this way… but the gcc command wasn’t there:
    “-bash: gcc: command not found” << this is what was written

    what 2 do?

  • wesg November 14, 2008, 6:47 pm

    @mad angel: This is likely because you do not have OS X Developer tools installed. This is not included in the stock installation, you need to insert the install DVD and choose “additional software”.

  • subramanya November 19, 2009, 4:49 am

    ware i can download the Compiled C for mac os x

  • Nadeem bari May 26, 2011, 7:26 pm

    it is ./a.out(enter) i think

  • MQ August 3, 2011, 8:10 pm

    this is what i did ~ mq_quirino$ /Users/mq_quirino/Documents/C practice files/Lorem Ipsum.c/Lorem Ipsum3.c ~/a.out
    then i got
    -bash: /Users/mq/Documents/C practice files/Lorem Ipsum.c/Lorem Ipsum3.c: Permission denied

  • Massaillon October 9, 2011, 4:51 am

    im trying to compile and execute ny c program using this method,but im getting this message error from the terminal: ” bash: gcc: command not found”

  • mvdhoef October 9, 2011, 1:56 pm

    Looks like GCC isn’t installed. Did you follow step #1? “Install Apple Developer Tools on the Mac OS X Install DVD by inserting Disk 1 and choosing Developer Tools.”

  • Aditya14all April 27, 2012, 9:54 pm

    It ain’t working on my mac.. i’m responded with a pop up saying ” you can’t save the fil with this extension” .. so wat else can I do??

  • Darwin May 5, 2012, 12:43 pm

    First u have to download gcc compiler… XCode first…..

  • ewb55 June 18, 2012, 10:46 am

    If you are working in your home directory, as is the case in the above example, then ~/a.out will work perfectly well. The command line of ./a.out will work from any directory where one is attempting to run their a.out. Explanation: The symbol tilde (~) refers to the user’s home directory. The symbol dot (.) refers to the current directory. By default, in Unix/Linux, the current directory is not in the command search path. Therefore, to execute something in the currrent directory, you have to explicitly give the directory in the command invocation. This is considered a security feature to thwart execution of imposter commands that someone may have placed into the directory that you just happen to be in. (This was not a characteristic of the Bell Labs Unix of the seventies.)

  • ennovY August 11, 2012, 6:09 am

    i got a message saying permission denied

  • Anshulbindal August 16, 2012, 7:56 am

    i have xcode but it still shows the same statement

  • Aditya Singhal September 13, 2012, 1:21 pm

    I have Mac OS X 10.8.1 and installed Xcode 4.4.1.I have also installed TextWrangler and write a code in C and save the file with .C extension.After that When I was trying to compile it by using
    gcc /Users/anirudhsinghal/Documents/ADS1.C
    it has displayed the error message as -bash: gcc: command not found

  • wesgood September 13, 2012, 1:25 pm

    Aditya, you probably need to install the Command Line Tools from within Xcode (Preferences > Downloads > Install). They’re not installed by default.

  • gandi siregar September 19, 2012, 5:35 pm

    Hello i’ve done it successfully thank you for the guide.
    But i Wonder how can i access the user folder.

  • danvip October 26, 2013, 8:02 pm

    To all of you who are having problems with gcc:
    -install xcode
    I’m not sure if they ask you right away if you want to install the developer tools if they automatically or if you have to select it on your own, but just install them.
    -then go to preferences>downloads>command line tools and install

    gcc should work now

Leave a Comment

Next Post:

Previous Post: