<img alt="Linux C Programming" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/linux-c-programming-1024×512.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="375" src="data:image/svg xml,” width=”750″>

Linux is becoming a developer’s programming paradise because it is an open-source operating system and freely available. The Turbo C compiler is already an old way to compile programs, so we programmers should switch to Linux to use a new programming environment. In this article, we will explain how to write, compile and run a simple C program. This will serve you as a foundation to move on to more complicated and useful C programs that you can write and run on Linux.

We have run the steps and commands mentioned in this article on an Ubuntu 22.04 LTS system, but it works exactly the same on other versions like Ubuntu 20.04 or distributions like Debian 11.

To compile a simple C program, we use the Linux command-line tool, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl Alt T.

Step 1: Install the build-essential packages

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

$ sudo apt-get install build-essential

<img alt="Install build-essential packages with apt" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-65.png62625e96110a3.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="355" loading="lazy" src="data:image/svg xml,” width=”481″>

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 Ubuntu’s graphical Text Editor and write or copy the following sample program into it:

#include

int main()
{
printf("nA sample C programnn");
return 0;
}

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

<img alt="Example C program" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-66.png62625e9631ac4.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="165" loading="lazy" src="data:image/svg xml,” width=”647″>

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.Advertisement

Step 3: Compile the C program with gcc Compiler

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

<img alt="Compile sourcecode with gcc" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-67.png62625e964f42c.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="56" loading="lazy" src="data:image/svg xml,” width=”468″>

Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths 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

<img alt="Start our compiled program" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-68.png62625e9666feb.jpg" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="103" loading="lazy" src="data:image/svg xml,” width=”272″>

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 Linux. All you need is the essential packages and the right skills to make you a programming guru in Linux!