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?
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With