Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run C program on Mac OS X

Tags:

c

I am learning C and wish to write the program using a text editor on my Mac (running OSX Lion 10.7). I write the .c file, and compile it using gcc filename.c - which creates executable file called a.out. However, when I type a.out or /a.out, I get the following messages: -bash: a.out: command not found or -bash: /a.out: No such file or directory. I have successfully compiled and ran C programs on Linux systems before using this same method. What am I doing wrong on my Mac?

like image 219
JT9 Avatar asked Dec 15 '12 00:12

JT9


People also ask

How do I run a compiled C program on a Mac?

To run the C program in MacOS we need a code editor and code compiler. The Code editor is used to write source code while the code compiler converts the source code into executable files. To write the source code Microsoft Visual Studio Code is used while to convert it into executable files we use command-line tools.

Does Mac Have C compiler?

Mac OS X comes with C built into it, and Apple has used C while making every aspect of OS X and iOS. Because C is such a popular language, it even forms the basis of many other programming languages, including two big names: C++ and Objective-C.

How do I compile and run a .C file?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.


2 Answers

You need to add a dot to indicate that the executable is in the current directory, as the current directory is not in the path:

./a.out 
like image 165
MByD Avatar answered Oct 19 '22 13:10

MByD


You need to precede a.out with ./ as follows:

./a.out 

Additionally, you may find it useful to change the name of the resultant executable. Example:

gcc nameofprogramyouwrote.c -o whatyouwanttheprogramtobenamed 

...then execute it like this:

./whatyouwanttheprogramtobenamed 
like image 41
beckah Avatar answered Oct 19 '22 14:10

beckah