Debian and Linux are becoming more and more popular for software developers as an open-source and free operating system. In this article we will explain how to write, compile and run a simple C program in Debian. This will serve as a basis for you to move on to more complicated and useful C programs that you can write and run on Linux.

We have performed the steps and commands mentioned in this article on a Debian 10-buster system and we will use the Linux command line, the terminal, to compile a simple C program.

Step 1: Install the C-Compiler and Build Tools

In order to compile and execute a C program, you need to have essential packages installed on your system. Enter the following commands as root in your Debian Terminal:

$ sudo apt-get update

(This command lets you install the latest version of a program from the Internet repositories.)

$ sudo apt-get install build-essential

Installing the C Compiler

You will be asked to enter the password for root; the installation process will begin after that. Please make sure that you are connected to the internet.

Step 2: Write a simple C program

After installing the essential packages, let us write a simple C program.

Open Debian’s graphical Text Editor, gedit, and write or copy the following sample program into it:

#include<stdio.h>
int main()
{
printf("\nA sample C program\n\n");
return 0;
}

Then save the file with .c extension. In this example I am naming my C program as sampleProgram.c

Sample Program

Alternatively, you can write the C program through the Terminal in gedit as follows:

$ gedit sampleProgram.c

This will create a .c file where you can write and save a program.

Step 3: Compile the C program

In your Terminal, enter the following command in order to make an executable version of the program you have written:

Syntax:

$ gcc [programName].c -o programName

Example:

$ gcc sampleProgram.c -o sampleProgram

Compile the C-Program with gcc (GNU C Compiler)

Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate path in this command.

Step 4: Run the program

The final step is to run the compiled C program. Use the following syntax to do so:

$ ./programName

Example:

$ ./sampleProgram

Run the compiled program

You can see how the program is executed in the above example, displaying the text we wrote to print through it.

Through this article, you have learned how to write, compile and run a simple C program in Debian 10 Buster. All you need is the build-essential packages and the right skills to make you a programming guru in Linux!

How to Write and Run a C program in Debian 10