How to Write and Run C Program on Linux

Wondering how to write and run C program on Linux?

Programming languages form the basis of every working computer system and application. A good grip over programming languages is considered an important skill to have in this day and age. This is why many people are learning multiple programming languages to broaden their skill set. 

One of the most basic and important languages to learn is C. C is a high-level programming language released back in the 1970s. It was developed as a programming language for Unix-operating systems. 

There are different Integrated Development Environments (IDEs) available in which people can practice and learn how to code. However, people who use Linux can write code with C using the Linux command terminal. This is quite convenient as some typical IDEs are quite large and consume a lot of system resources.

This article is a guide on exactly how to write and run a C program on Linux. You just have to go through a few easy steps, and then you can be on your way to writing a C program. 

So, let’s get going!

Step 1: Install Build-Essential Package. 

The Build-Essential packages are meta-packages that are necessary for your Linux system to have if you want to compile software or codes in any programming language. It contains essential tools such as debuggers and compilers. 

To install Build-Essential packages, you need to first update your system repositories to get the latest versions of the packages available. You can do that using the following command.

$ sudo apt update
run c program on linux

After updating the repositories, use the following command to install the Build-Essential packages. 

$ sudo apt-get install build-essential
build essentials

You should see the following progress after you press enter and execute the command. 


Step 2: Write a Program in C.

After installing the build-essential packages, you are all set to write and run your desired program in C. To do so, you need to use any of the Linux terminal text editors that are available to you. The most basic one is the “nano” text editor, and we will be using that. If you also want to use nano, you can enter the following command.

$ sudo nano <programname.c>
run c program on linux

Next, you can proceed to write a code in C. For example,

int main()
 printf(“This is an example of a C program\n”
printf(”Hello World!\n”);
printf(”Hello World!\n”);
 return 0;

After writing your desired program, save it and close it. 

Step 3: Compiling the Program.

After writing the program, the next step is to compile it. In the compilation process, the compiler checks the whole program for potential errors. In case there are no errors, the high-level language is converted to its equivalent binary code that the computer can understand.

For the terminal, you can compile a C program using the gcc compiler, which is very simple to execute and use. The syntax for using the gcc compiler is as follows. 

$ gcc <programname.c> -o <programname>
run c program on linux

On pressing enter, you should see the terminal cursor go to the next line as a sign of successful execution.

Step 4: Running the Program.

After writing and compiling a C program, the final step of the puzzle is to run the program you have written. The command that you have to write for this is pretty simple, and it is as follows. 

$ ./<programname>

On pressing enter, you should also see that the program you wrote has been successfully executed, as you can see in the image below. The program we wrote was also successfully executed, and we got the desired output. 

run c program on linux

These were the steps on how to write and run a C program on a Linux command terminal. Alternatively, there are also other ways to run C programs on Linux as well. 

Alternative Methods.

As mentioned above, programmers typically use code IDEs to write and compile programs in any programming language. One of the most popularly used IDEs nowadays is the Visual Studio Code developed and provided by Microsoft. Although it is owned by Microsoft, it does provide Linux-supported versions of Visual Studio Code for prospective Linux users. 

If you want to use Visual Studio, you can download it using this link.

There are other IDEs such as CodeBlocks and Eclipse available as well for developing software in C. However, these IDEs are typically used for developing large projects. Other IDEs include Dev C++, CLion, Codelite, and NetBeans. All these are viable C IDEs that you can choose to use on Linux. 

As we are talking about programming languages and C in specific, let’s take a look at what shell scripts are. Shell scripts are tiny blocks of code that house certain commands. These can be pre-built and also made by the users themselves. 

Consider a case where you must write a string of certain commands repeatedly while doing your work. It can prove to be annoying writing them repeatedly. This is where shell scripts can come in handy. 

You can write a shell script in which all the commands you use on repeat are included. Then, you can just use the script’s name, and the commands will be executed automatically. Simple. If you want to learn more about shell scripts and how you can write them, click on the link, and it will take you to a comprehensive guide. 

In this article, we looked at how to write and run a C program on Linux. We mentioned two ways, which are the command terminal and IDEs that you can use to program in C. Learning programming languages makes you know more about how computers function and how the applications we use work beneath the user interface. 

We hope that we can help you understand the necessary steps and that you are now having fun coding in C. For further queries, please let us know in the comments below.

If this guide helped you, please share it. 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *