How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner's Tip]

Running C program in Linux command line is not that difficult. Running it in a code editor like Visual Studio Code is even easier. Learn both methods.
Warp Terminal

How do you program in C on Linux? It is indeed very easy and consists of three simple steps.

Step 1: Write your program and save the file with a .c extension. For example, my_program.c.

Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this:

gcc -o my_program my_program.c

Step 3: You run the generated object file to run your C program in Linux:

./my_program
Running a C Program in Linux using gcc
Running C Program in Linux

This was just a quick summary of how to compile and run a C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail to make you comfortable coding C programs in a Linux environment.

I’ll discuss how to run C programs in a Linux terminal and a code editor.

Method 1: How to run C programs in a Linux terminal

In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection).

You can install gcc using your distribution package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:

sudo apt install gcc

Switch to the directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:

gcc -o my_program my_program.c

Keep in mind that it is optional to provide the output object file (-o my_program). If you won’t do that, an object file named a.out will be automatically generated. But this is not good because it will be overwritten for each C program and you won’t be able to know which program the a.out object file belongs to.

Once your object file is generated, run it to run the C program. It is already executable. Simply use it like this:

./my_program

And it will display the desired output if your program is correct. As you can see, this is not very different from running C++ programs in Linux.

An SVG anmation showing the installation of gcc and execution of a simple C program in a Linux terminal
Running a C Program in a Linux terminal
📋
Every time you make a change in your program, you have to compile it first and then run the generated object file to run the C program.

Method 2: How to run C programs in Linux using a code editor like Visual Studio Code

Not everyone is comfortable with the command line and terminal and I totally understand that.

You can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often too heavy programs and more suitable for large projects.

I recommend using an open-source code editor like Visual Studio Code or Atom. These are basically text editors and you can install add-ons to compile and run programs directly from the graphical code editor.

I am using Visual Studio Code editor in this example. It’s a hugely popular open-source code editor from Microsoft.

First thing first, install Visual Studio Code in Ubuntu from the software centre. For other distributions, please check your Linux distribution’s package manager or software centre. You may also check the official website for more information.

Start Visual Studio Code and open/create a project and create your C program here. I am using a sample Hello World program.

C program editing in Visual Studio Code
C Program in Visual Studio Code

You must ensure the gcc compiler is installed on your Linux system.

sudo apt install gcc

Next, you would want to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for the C/C++ programs but it is complicated to set up and hence I won’t recommend it.

Instead, I suggest using the Code Runner extension. It’s a no-nonsense extension and you can run C and C++ code easily without additional configuration.

Go to the Extensions tab, search for ‘Code Runner,’ and install it.

Installing Code Runner extension in Visual Studio Code
Install Code Runner extension for running C/C++ program

Restart Visual Studio Code. Now, you should be able to run the C code by using one of the following ways:

  • Using the shortcut Ctrl+Alt+N.
  • Press F1 and then select or type Run Code.
  • Right-click the text editor and click Run code from the context menu.
Right -click the program file that you want to run and then press "Run Code" to run that code
Right-click the program file and choose Run Code

When you run the program, it is compiled automatically and then run. You can see the output in the terminal that is opened at the bottom of the editor. What could be better than this?

Program output is displayed in the bottom section of the Visual Studio Code editor
The program output is displayed in the bottom section of the editor

Which method do you prefer?

If you prefer a video to learn about installing and running C programs in Linx, check out the below Video tutorial.

Run C Program in Linux

Running a few C programs in the Linux command line is okay but using a code editor is much easier and saves time. Won’t you agree?

I will let you decide whichever method you want to use.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.