115

I am new to C. Here is my "Hello, World!" program.

#include <stdio.h>

int main(void)
{
  printf("Hello, World!\n");
  return 0;
}

After I try to run it using Terminal it says:

/Users/macbook/Desktop/peng/Untitled1

-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied

Why?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bolat Tleubayev
  • 1,765
  • 3
  • 14
  • 16

10 Answers10

212

First save your program as program.c.

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 can you find App Store? Do a "Spotlight Search" by typing Space and start typing App Store and hit Enter when it guesses correctly.

App Store looks like this:

Enter image description here

Xcode looks like this on App Store:

Enter image description here

Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type Space and start typing Terminal and hit Enter when it guesses Terminal.

Now install the command-line tools like this:

xcode-select --install

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:

gcc -Wall -o program program.c

Note: On newer versions of OS X, you would use clang instead of gcc, like this:

clang program.c -o program

Then you can run it with:

./program
Hello, World!

If your program is C++, you'll probably want to use one of these commands:

clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 6
    Make it `gcc -Wall -o program program.c` and you'll get helpful compiler warnings (though not all of them) that will tell you some of the reasons your code isn't working. – Schwern Jan 06 '17 at 20:31
  • Nice answer, but note that there is no need to explicitly invoke clang - gcc is symlinked to clang anyway. – Paul R Aug 22 '17 at 08:26
  • And plain `cc` is symlinked to both of them — that's all I ever have to type. – Steve Summit May 10 '22 at 21:06
64

First make sure you correct your program:

#include <stdio.h>

int main(void) {
   printf("Hello, World!\n"); //printf instead of pintf
   return 0;
}

Save the file as HelloWorld.c and type in the terminal:

gcc -o HelloWorld HelloWorld.c

Afterwards, just run the executable like this:

./HelloWorld

You should be seeing Hello, World!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
17

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. You need a compiler for that.

Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)

  • 5
    best answer! `A "C-program" is not supposed to be run.` – Eddie Jan 04 '19 at 10:30
  • I believe a reasonable answer to this SO question should include instructions on how to compile and execute a program written in C. This answer does not contain any such instructions, therefore downvote. Please let me know if you disagree. – Alex Nov 25 '21 at 17:02
  • Alex, you may feel free to add another answer. – gnasher729 May 10 '22 at 21:25
16

This is Working in 2019

By default, you can compile your name.c using the terminal:

 cc name.c

And if you need to run, just write

 ./name.out
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antonio Cachuan
  • 475
  • 1
  • 9
  • 22
  • 3
    its showing up as "a.out" instead of f"{name}.out" no matter what the name is. its always "a" for some reason. – Flaming_Dorito Mar 26 '19 at 23:07
  • 1
    @Flaming_Dorito, that's the default behaviour to create the executable object file. You can however choose to give it a name of your liking. Type -> cc -o name name.c – Yash Tamakuwala Jun 03 '19 at 14:09
  • @YashTamakuwala ohh I see. Thanks – Flaming_Dorito Jun 03 '19 at 17:13
  • Using `cc` is unusual. Presumably it is some kind of alias. Perhaps add a reason and a source for it (e.g., to some standard documentation)? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen May 10 '22 at 20:46
  • cc works on OSX Monterey. `which cc` tell me `/usr/bin/cc` and `file /usr/bin/cc ` says `/usr/bin/cc: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64` [...] – Antonio Aug 28 '22 at 12:50
7

To do this:

  1. Open the terminal

  2. Type in the terminal: nano ; which is a text editor available for the terminal. When you do this, something like this would appear.

  3. Here you can type in your C program

  4. Type in Ctrl + X → which means to exit.

  5. save the file by typing in Y to save the file

  6. Type the file name; e.g., helloStack.c (don't forget to add .c)

  7. When this appears, type in gcc helloStack.c

  8. And then ./a.out: this should give you your result!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

For compiling a C program on your latest macOS, just type the following in the terminal after saving the file with a .c extension and on reaching the path where the file is saved:

cc yourfilename.c

Once you have checked all the errors after compilation (if any), type the following for executing the code:

./a.out

These commands are tested on macOS v10.14 (Mojave) and are working perfectly fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

To compile a C program in macOS, simply follow the below steps

Using the cd command in terminal, go to your C program location and then type the command present below:

make filename

then type

./filename
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

The answer is chmod 755 hello - it makes the file executable... I had same problem on macOS, which is now solved.

nano hello.c
make hello
chmod 755 hello

Then you run it by ./hello

clang --version

Output:

Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0

Nothing was installed. nano make (clang) chmod - all inside macOS already.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What is the purpose of the last sentence? Can you elaborate? Preferably by [editing](https://stackoverflow.com/posts/64616916/edit) (changing) your answer. – Peter Mortensen May 10 '22 at 20:57
0

On Mac, GCC (executable gcc) is installed by default in /usr/local/bin.

To run C:

gcc -o tutor tutor.c
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Re *"To run C"*: Don't you mean *"To compile"* or *"To run GCC"*? Or *"To compile a C program"*? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/66201243/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 10 '22 at 21:00
  • Actually, no, it isn’t. Gcc hasn’t been part of Xcode for ages, but clang is. And there is an alias that will call clang if you try to call gcc. – gnasher729 May 10 '22 at 21:29
-2
  1. First you need to install a GCC compiler for Mac (google it and install it from the Internet)

  2. Remember the path where you are storing the C file

  3. Go to Terminal and set the path

    E.g., if you have saved in a new folder ProgramC in the Document folder.

    Then type this in Terminal:

    cd Document
    cd ProgramC
    
  4. Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)

  5. Now compile your program

    make Hello
    ./hello
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131