Homehow ToHow to run a program in C on Linux: A Beginner's Guide

How to run a program in C on Linux: A Beginner's Guide

Linux is a paradise for developers because it is open source and free. The Turbo C compiler is an old approach so we moved to Linux for a sophisticated programming environment. In this article, we'll explain how to write, compile, and run a program in C. Basically, we present to you the basis for shifting to more complex and useful C programs.

The steps below ran Ubuntu 18.04 LTS.

We will use the Linux command line and the Terminal to compile a simple program in C. To open the Terminal, you can use Ubuntu Dash or press Ctrl + Alt + T.

Step 1: Install the basic packages

In order to compile and run a program in C, it is necessary to have the basic packages installed. Run the following command in Terminal:

$ sudo apt-get install build-essential 

Linux program in C

You will need to enter the root password to get started installation. Make sure you are connected to the internet throughout the process.

Step 2: Write a simple one C program

After installing the packages, let's write a simple program.

Open Ubuntu Text Editor (with graphical interface) and write or copy the following program:


int main ()


printf ("\ nA sample C program \ n \ n");

0 return;


Then save the file with .c extension. In this example, the program is called sampleProgram.c

program in C Linux

Alternatively, you can write the C program from Terminal to gedit, as given below:

$ gedit sampleProgram.c

This will create a .c file in which to write and save the program.

Step 3: Compile with gcc

In Terminal, run the following command to create an executable version of the program.


$ gcc [programName] .c -o programName


$ gcc sampleProgram.c -o sampleProgram

Make sure your program is in the Home folder. Otherwise, you will need to enter the appropriate command paths.

Step 4: Run the program

The last step is to run the compiled program. To do this, we use:

$ ./programName


$ ./sampleProgram

You can see how the program was executed in the example above.