How To Run C Program On Mac Tutorial 2019

How To Run C Program On Mac Tutorial 2019 is the topic, we will discuss today. Mac OS X makes it easy to run the C Programs. Right now, I am using Mac OS X Mojave which is the latest version. Also, I am using XCode’s GCC compiler, and XCode’s release is right now 10. So, if you are not using Mojave, then my suggestion is to upgrade your OS to Mojave and then update your XCode so that You will get the maximum benefit from Apple Development Team to get new features. If Mojave is unsupported to your Macbook then also, you can use this post to run the C programs. If you have an XCode, then you have the C compiler.

How To Run C Program On Mac Tutorial 2019

You can check the latest C version by typing the following command in your terminal.

clang --version

 

How To Run C Program On Mac 2019

The “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.

If we need to develop C programs, then there are two things that we need:

  1. First, we need the code editor – this is the program that we will use to write our source code (a source code file is merely the text file, which has the “.c” extension, and which contains the valid C code). We are using Visual Studio Code as an editor.
  2. Secondly, we need the C compiler – this is the program that converts a source code that we have written into an executable file that we can run. We are using XCode’s default compiler.

Now, open a code editor. I am using Visual Studio Code because it has excellent support for C, C++ languages. So let us spin up the visual studio code and create one file called helloworld.c and add the following code inside it.

#include <stdio.h>

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

Save the file, and now first we need to compile the code.

To do that, go to the terminal and go to the root of our project and type the following command. You can use any of the commands to compile the C file.

gcc helloworld.c

# or

clang helloworld.c -o helloworld

It will generate one more file in the same directory called a.out

Now, this file is the compiled file, and now we need to run this file to get the output in the terminal.

So again type the following command and hit the enter key or return key.

./a.out

Now, it prints the output in the terminal.

 

Run C Program On Mac

Errors in C Program

Now, let us say we have any syntax error in our program like the following code.

#include <stdio.h>

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

If your editor does not have linter, then it won’t tell you the error in the editor then you will realize when you compile the program. So let us compile it and see what we got. We know that we have missed the semicolon.

 

Compile C Program In Macbook

We will see the error like this, and that error clearly says that we have missed the semicolon at the end of the line.

Remember, when you change in the code, you will need to recompile otherwise the changes are not reflected in the output.

I am using Visual Studio Code and I have installed the linter extension for C, C++ language. So the syntax error will show up on the Editor like this.

 

C in Visual Studio Code

Finally, How To Run C Program On Mac Tutorial 2019 is over.

Post Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.