How to write and run a C program on Linux

Linux is becoming a haven for developers, being an open and free operating system. The Turbo C compiler is an old approach to compiling programs, so let programmers switch to Linux for a new programming environment. In this article, we will describe how to write, compile, and run a simple C program. This will serve as the basis for your transition to more complex and useful C programs that you can write and execute on Linux.

We followed the steps and commands mentioned in this article for the Ubuntu 18.04 LTS system, but it will work the same way with other distributions such as Debian 10.

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

Step 1. Install the packages required for assembly

To compile and execute the program in C, you need to install the necessary packages on your system. Enter the following command as root in your Linux terminal:

$ sudo apt-get install build-essential

You will be asked to enter the password for the root user; After that, the installation process will begin. Please make sure you are connected to the Internet.

Step 2: Write a Simple C Program

After installing the necessary packages, we will write a simple C program.

Open the Ubuntu graphical text editor and write or copy the following sample program into it:


int main()
printf("nA sample C programnn");
return 0;

Then save the file with the extension .c. In this example, I call my C program as sampleProgram.c

Program Example C

Alternatively, you can write a C program through a terminal in gedit as follows:

$ gedit sampleProgram.c

This will create a .c file where you can write and save the program.

Step 3: Compile a C Program Using gcc Compiler

Enter the following command in your terminal to create an executable version of the program you wrote:


$ gcc [programName].c -o programName


$ gcc sampleProgram.c -o sampleProgram

Compile the source code with gcc

Make sure your program is in your home folder. Otherwise, you will need to specify the appropriate paths in this command.

Step 4: Run the program

The final step is to run the compiled C program. To do this, use the following syntax:

$ ./programName


$ ./sampleProgram

Run our compiled program

You can see how the program runs in the above example, displaying the text we wrote to print through it.

In this article, you learned how to write, compile, and run a simple C program on Linux. All you need is the necessary packages and the necessary skills to make you a Linux programming guru!

How to write and run a C program on Linux