Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run C program on Mac OS X using Terminal?

Tags:

c

bash

macos

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?

like image 938
Bolat Tleubayev Avatar asked Sep 01 '15 17:09

Bolat Tleubayev


4 Answers

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
like image 200
Mark Setchell Avatar answered Nov 10 '22 09:11

Mark Setchell


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!

like image 30
Eduard Grigorescu Avatar answered Nov 10 '22 10:11

Eduard Grigorescu


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

like image 16
Pieter Bruegel the Elder Avatar answered Nov 10 '22 10:11

Pieter Bruegel the Elder


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
like image 15
Antonio Cachuan Avatar answered Nov 10 '22 08:11

Antonio Cachuan