How to run c program in mac terminal

This is a good example for compiling single-file code, but in the event that you have multi-file code or want to explicitly include headers during compilation, your best bet is to separate the compilation process and use a makefile for speed. The make tool only recompiles code that has changed, which can shorten the compilation process greatly when you have several files. Without make: Compiling an object file per each source file 1st step:.

Also, some versions of gcc seem to include them for you automatically, but not including them explicitly can cause problems when compiling on other platforms. With make: For a makefile, simply create a file called "makefile" no extension , and use this syntax to create a rule:. You can use "make executable" to instruct the make tool to run all commands necessary to create executable, and it will only recompile whatever code has changed since you last ran make. If you simply run "make", it will only process the first rule entry, so it's good practice to include your top-level executable as the first rule.

Additional rules can be made to simplify things, such as creating rules to run the program with specific arguments, or to clean up temporary files, or clear out previous compiled files.


  • Developing C programs on Mac OS.
  • Asking conceptual questions!
  • Welcome to LearnProgramming!.
  • Your Answer?

Samples include:. There's a lot more that you can do with the make tool, but this is sufficient for simple or self-made projects. Also, if you are a little lazy, as I am sometimes, if you have all of your source files in one directory, using a wildcard can to compile all the source files at once such as this:.

This is easier then writing out every file but you will have to compile every file again unlike a makefile. On a Mac, you probably want to use clang instead of gcc. Apple stopped updating the version of gcc after 4.

Installing Visual Studio Code

Yeah I'm using clang 3. Clang is surprisingly much better and faster than gcc. Word of advice though, I would use clang which is the default compiler that comes with xcode these days, not only are their numerous advantages to this compiler you'll see later, but it doesn't really care what flavour of C you are writing in so long as you name your file extensions correctly.

You need to install the xcode command line tools. Have you got xcode installed? These will allow you to call the gcc complier from your command line. In any case, some links:. Use of this site constitutes acceptance of our User Agreement and Privacy Policy. All rights reserved.

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

Want to join? Log in or sign up in seconds. Submit a new text post. Get an ad-free experience with special benefits, and directly support Reddit. Posting guidelines Frequently asked questions Subreddit rules Message the moderators Asking debugging questions If you need help debugging, you must include: A concise but descriptive title. A good description of the problem. A minimal , easily runnable , and well-formatted program that illustrates your problem.

The output you expected, and what you got instead. If you got an error, include the full error message. Asking conceptual questions Many conceptual questions have already been asked and answered. Other guidelines and links Frequently asked questions Asking homework questions Asking for code review Answering questions Learning resources Other communities Subreddit rules 1. Abusive, racist, or derogatory comments are absolutely not tolerated. No spam or tasteless self-promotion When posting some resource or tutorial you've made, you must follow our self-promotion policies. No off-topic posts Do not post questions that are completely unrelated to programming, software engineering, computer science, and related fields.

Do not ask exact duplicates of FAQ questions Do not post questions that are an exact duplicate of something already answered in the FAQ. Do not delete posts Do not delete your post! Use the "solved" flair instead. No rewards You may not ask for or offer payment of any kind monetary or otherwise when giving or receiving help. No indirect links All links must link directly to the destination page.

Udemy coupon links are ok: Do not promote illegal or unethical practices Do not ask for help doing anything illegal or unethical. No complete solutions Do not ask for or post a complete solution to a problem. Welcome to Reddit, the front page of the internet. Become a Redditor and subscribe to one of thousands of communities. By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools.

How To Compile C/C++ on Mac OS X

How to find App Store? Then you need to install the command-line tools in Terminal. How to start Terminal? Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:.

On newer versions of OS X, you would use clang instead of gcc , like this:. A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal.

Bonus method

You need a compiler for that. Oh, and the answer to your last question "Why? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies. Ask Question. Here is my "Hello,world! What is file "Untitled1"? Is the file who contains the source you have included in the question or anything else?

macos - How to run C program on Mac OS X using Terminal? - Stack Overflow

Replace pintf by printf , keep int and main on the same line. First save your program as program. App Store looks like this: Xcode looks like this on App Store: Now install the command-line tools like this: