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]

20 comments on “How to: Write and compile C programs on Mac OS X
  1. mvdhoef says:

    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)

  2. mad angel says:

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

    what 2 do?

  3. wesg says:

    @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”.

  4. subramanya says:

    ware i can download the Compiled C for mac os x

  5. Nadeem bari says:

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

  6. MQ says:

    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

  7. Massaillon says:

    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”

  8. mvdhoef says:

    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.”

  9. Aditya14all says:

    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??

  10. Darwin says:

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

  11. ewb55 says:

    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.)

  12. ennovY says:

    i got a message saying permission denied

  13. Anshulbindal says:

    i have xcode but it still shows the same statement

  14. Aditya Singhal says:

    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

  15. wesgood says:

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

  16. gandi siregar says:

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

  17. danvip says:

    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

  18. fifiey says:

    i have done as per instructed but it keeps popping back –> undefined symbols for architecture x86_64

  19. fifiey says:

    yet i’m using c not c++

  20. For those getting “gcc: command not found” try “cd /usr/bin” first