How to Compile and Run C Program on a Linux Terminal

How to Compile and Run C Program on a Linux Terminal


This post was last updated on April 17th, 2021 at 11:08 pm

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.

Previous How to Install and Run Ubuntu on Windows 10 / 7 / XP - A Step By Step Guide
Next How to fix Python ModuleNotFoundError: No module named 'tkinter' in Linux?

About author

shibaa987 274 posts

Linux kernel developer and a firmware developer with an experience of 10+ years.

View all posts by this author →

You might also like

Linux Terminal 0 Comments

How to create a file in the Linux terminal?

A file on a computer stores data, information, settings, etc. On a Linux system, you can create a file in so many ways for instance if you are using GUI

Linux Terminal 0 Comments

How To Install and Configure Z Shell (zsh) in Ubuntu/Linux Mint?

This post was last updated on August 1st, 2020 at 09:16 pmIn a Unix based operating system, a shell is a program that interprets the command and acts as an

Linux Terminal 0 Comments

How To Install And Use FISH Shell In Ubuntu/Linux Mint?

This post was last updated on August 1st, 2020 at 04:57 pmI have already discussed the installation and configuration of the Z shell in Ubuntu/Linux Mint which is quite similar


No Comments Yet!

You can be first to comment this post!

Leave a Reply