How to Compile and Run C Program on a Linux Terminal

0/5 Votes: 0
Report this app


Linux is the best platform to learn C programming because it is an open source platform and you will find so many documentation on the internet if you need help.

The second reason is that it provides built-in C compiler to start running a C program.

In the series of guides, till now I have explained C programming basics with a small Hello World program.

Although there are many IDEs you can use but in this guide I am going to show you how to compile and run a C program using a Linux Terminal.

Required Setup

You can run a C program on a Linux machine in one of the below ways:

Choose how you want to run your C development setup to proceed.

To keep it simple and relevant for most beginners, I will use Ubuntu installed via Oracle Virtualbox on a Windows PC.

House Keeping Before Starting

Before creating and running a C program, let’s do some management work which will help us find all the C programs in one place.

I would recommend you to create a new directory where you keep all the C programs.

To do that, go to your home directory on Ubuntu and create a directory using the mkdir command, let’s say in the name of c-programming.

  1. First open the terminal window on Ubuntu (or Linux in general) using the below keyboard combinations:
    Ctrl + Alt + T

    You can also use the respective menu items or a search box if provided in the Linux distro you are using.
  2. Then execute the below commands on the terminal to create the directory:
    cd ~
    mkdir c-programming
  3. You can also use the ls command to verify if the directory is created or not:
    ls -l

    create and list directory

  4. Once the directory is created, let’s change our working directory to the new one. You can use the cd command to change the directory and then the pwd command to verify the present working directory in Linux.
    cd c-programming

    check pwd

Once you have created and moved inside the directory let’s create compile and run our first C program on the Linux Terminal.

The C Program to Compile and Run on Linux Terminal

For the sake of simplicity I will use the first Hello program that I wrote earlier in C.

Create the hello.c file

  1. First create a file in VIM editor in the name of hello.c using the below command:
    sudo apt-get install vim
    vim hello.c


    The above command first installs the VIM editor and then creates the file.

  2. Once created the file and you are in the VIM editor screen, press the I button on your keyboard to run VIM in writing mode.
  3. While in the writing mode, copy and paste the below Hello World C program code in the hello.c file in VIM:
     * This program is demo code from
     * Author:
    #include <stdio.h>
    /* The entry point function in C */
    int main ()
        /* The actual print statement */
        printf ( "Hello, World!" );
        return 0;
  4. Once copied the code, press Esc to move out of editing mode.
    Then :wq! and then press Enter to exit

    Create Hello World C program in VIM

Compile the hello.c file on the Terminal

  1. Execute the below command to compile the C file using GCC compiler:
    sudo apt-get install gcc
    gcc hello.c


    The above command first installs the GCC compiler and then compiles the hello.c file.

  2. After successful compilation, the compiler will generate an a.out executable file.
  3. You can use the ls -la command to see the binary file is generated or not.

    gcc hello world c program

Run the output binary file in Linux Terminal

  1. Once the output binary file is created, execute the below command to run the C program that we created:
  2. The output:
    Hello, World!

That’s how you need to use the Linux terminal to compile and run a C program.

I hope that was quite straight forward and simple for you to understand. If you still have doubts on something, please leave a comment and I will try to address your concern(s) as soon as possible.

For more detailed step-by-step C programming and other Linux guides, keep visiting Explore Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *