How to Run C Programs in Linux? – 3 Step Quick Guide

In this tutorial, we will learn how we can run C programs in Linux. C language was developed at Bell Laboratories in 1972 by Dennis Ritchie. It is still very popular for system-level programming. C language is the mother language of many other languages.

In fact, Unix is one of the first operating system kernels that is implemented using C.

Steps to Run C Programs in Linux

To run a C program in Linux terminal you need to first install a compiler for C. Let’s go over it step by step.

1. Install build-essential package on your Linux System

To install the essential packages the steps are as follows Run a sudo apt update as shown below

sudo apt update

To install the essential packages in your system run the command :

sudo apt install build-essential
Install Essential Packages
Install Essential Packages

This command will install all the necessary packages for running a C program.

2. Write a C program to test

There are multiple options when it comes to writing a C program. You can use a GUI code editor to write or you can use the terminal. Even when writing the code on the terminal, there are multiple options available. In this tutorial, we will create a C program using the Vim editor. To open a file in vim editor use the command:

vim c_code.c 
Vim

The code used in the example is as follows:

#include <stdio.h>
int main()
{
   printf("Linux For Devices!");
   return 0;
}

To save and quit vim type:

:wq [Enter]

To verify the contents of the file use cat command:

cat c_code.c
Cat C Code

3. Compiling and Running

To compile the C program we will use GCC compiler. Check the version of your GCC compiler use the command:

gcc --version
Gcc Version

To compile the C program use the following command:

 gcc c_code.c -o c_code

If there is no error then the terminal will give no output. If there is an error, the terminal will display it. To run the C program use the command:

./c_code 
C Program Output
C Program Output

Terminal gives the output shown above. To save the output in a file use the command:

./c_code > c_code_output.txt

This will save the output in the file. To view the file use cat command:

cat c_code_output.txt
File Output

Conclusion

Here we’ve seen how we can run C programs in Linux. Now you can use C language to create useful programs and then run those programs easily in Linux.