How to Compile and Run C Program in Ubuntu

Linux is getting popular in programming for developers because of being a free and open-source operating system. In Linux, most of the programming software and compiler comes preinstalled.

There are many popular compilers available for the C programming language in the market, for example, GCC Compiler, Clang, MinGW, Turbo C, etc.

In this guide, we will be more focused on GCC Compiler, and all of the steps from compiling to running C programming are performed on Ubuntu 20.10, which is Debian-based distribution.

Compile and Run C Program in Ubuntu

To run and compile the C program properly in your Linux system, you need to install build-essential package, which include GCC/g++ compilers and libraries which are required to compile and run C programs in Linux.

$ sudo apt install build-essential

After installation is complete, all required packages will be installed in your system.

Now we write a simple basic C program to print LinuxShellTips on screen.

#include <stdio.h> 
int main() {
   // printf() displays the string inside quotation
   printf("\nLinuxShellTips\n\n");
   return 0;
}

After you are done, save your file with a .c extension. In this example, I have saved with the name firstProgram.c.

In order to build an executable file for your program, enter the below command in your terminal. Make sure you are on the correct path where you have saved your program in your system.

$ sudo gcc firstProgram.c -o firstProgram

A new executable file with the name firstProgram will be created in the same path you have executed the above code.

Time to run your first program by running the below command in your terminal to execute your first program.

$ ./firstProgram

LinuxShellTips

That’s how you compile and run a C program in your Linux System. If you have any queries, feel free to ask in the comment section.

Vivek Gagan
Innovative tech mind with 10 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.

Each tutorial at LinuxShellTips is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How to Compile and Run C Program in Ubuntu”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.