How to Compile and Run C Program in Linux

Writing and compiling C Programs in Linux is fairly easy in Linux.

C is a general-purpose programming language, best suited for system programming. Dennis Ritchie developed it at Bell Lab to write utility programs for the Unix Operating System. Later, the Unix kernel itself was implemented in C Language.

Low-level access to memory, simple keywords, and fast and efficient execution features made C one of the most used programming languages. Learning a C is a must if you are starting your career as a programmer.

You only need a text editor to write a C Program and GCC or Clang compiler to compile the program.

Pre-requisite:- Install GCC Compiler in Linux if not available

GCC compiler is part of the GNU Compiler Collection that includes front ends for C, C++, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages (libstdc++,…).

This compiler is part of the build-essential package for Ubuntu/Debian-based Distros and the Development Tools package for Fedora/Centos/Rehat-based Distros. It should already be available on your favorite distro.

Run either of the below commands to check if it is there

  • gcc -v
  • whereis gcc
  • which gcc

If that is not the case, first you need to install GCC on different Linux Distros using the below steps.

Sr.No Linux Distro Commands
1 Ubuntu/Debian-based sudo apt-get install build-essential
2 Fedora/Centos/Rehat-based yum groupinstall ‘Development Tools’
3 Arch Based sudo pacman -S gcc

Write, Compile, and Run C Programs in Linux

Now, pre-requisite GCC compiler install is complete. Let’s move to our main topic.

1. Write C Program

Any text editor like vi, nano, or even VIM is fine to write a C program. Here, I will create a classic Hello World program using a nano editor.

Open the terminal and start the editor.

nano hello-world.c

Put below C program listing in the file and save.

#include <stdio.h>
int main()
{
printf("/n Hello World ");
return 0;
}

Where,

  • studio.h – standard input output header file
  • printf – command to print a message

2. Compile C Program with GCC Compiler

Next, compile the program using the GCC compiler. The syntax is gcc <file name> -o <output file>. In our case,

gcc hello-world.c -o hello-world

This generates a hello-world binary file.

3. Run C Program in Linux

Now, just Type ./<binary output filename> to run the program.

~$ ./hello-world

The output of the program ( shown below),

c program output when run on linux terminal
c program output

Summary

In conclusion, compiling and running C programs in Linux is a straightforward process that any developer should be familiar with. By leveraging the powerful GCC compiler and understanding the basic commands, you can easily create, compile, and execute your C programs in a Linux environment.

Throughout this tutorial, we’ve covered the essential steps, from writing your C source code to using the terminal for compiling and running the program.

I hope this small article was helpful. Please share and subscribe.

Scroll to Top