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 
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.

Got something to say? Join the discussion.