C Question

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

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

#include <stdio.h>

int
main(void)

{
pintf("Hello, world!\n");
return 0;
}


After I try to run it using Terminal it says:

Last login: Tue Sep 1 23:19:47 on ttys000
/Users/macbook/Desktop/peng/Untitled1
MacBook-Pro-MacBook:~ macbook$ /Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied
MacBook-Pro-MacBook:~ macbook$


Why?

Answer

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. It looks like this:

enter image description here

Then you need to install the command-line tools with this command in Terminal. This allows you to simply run gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:

xcode-select --install

Then you can compile your code with

gcc -o program program.c

Then you can run it with

./program
Hello, world!