How to write and run a C program in Debian 10

s2 0

Debian and Linux are becoming increasingly popular with software developers as open source and free operating systems. In this article we explain how to write, compile and run a simple C program in Debian. This will serve as a foundation 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 will be using 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 run a C program, important packages must be installed on your system. Enter the following commands as root in your Debian terminal:

$ sudo apt-get update

(You can use this command to install the latest version of a program from the Internet repositories.)

$ sudo apt-get install build-essential

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

Step 2: write a simple C program

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

Open the Debian graphical text editor, gedit, and write or copy the following sample program into it:

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

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

Alternatively, you can write the C program in gedit via the terminal as follows:

$ gedit sampleProgram.c

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

Step 3: compile the C program

In your terminal, enter the following command to create an executable version of the program you wrote:advertising

Syntax:

$ gcc [programName].c -o program name

Example:

$ gcc sampleProgram.c -o sampleProgram

Compile the C program with gcc (GNU C Compiler)

Make sure your program is in your home folder. Otherwise, you must specify the 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 this:

$ ./programname

Example:

$ ./sampleProgram

Run the compiled program

You can see the program in the example above run by displaying the text we wrote to print it.

Through this article you learned how to write, compile, and run a simple C program in Debian 10 Buster. All you need are the Build Essential packages and the right skills to turn you into a programming guru in Linux!

How to write and run a C program in Debian 10