Write, Compile and Run C Program In Linux With GCC

Write, Compile and Run C Program In Linux With GCC (C program Compiler)– Linux is the best operating system for Programmers and Developers. Developers prefer open-source operating systems for developing computer programs and Linux is the most popular open-source operating system. Well, In this tutorial you will learn how to Compile C Program In Linux with GCC.

After going through this tutorial, you will learn how to write your first ever C Program on your Linux Machine, and Compile and Run it.

Well, there are so many Linux distributions available today. So, I have tried my best to make this tutorial blog post useful for all Linux Distributions.

I am using 3 Linux distributions to explain how to Compile C Programs in Linux. Most of the Linux Operating systems that are available today are based on one of these Operating Systems- Red Hat, Ubuntu, or Arch Linux.

No matter which Linux Operating System or Linux Distro you are using but you will be able to compile a C program after going through this article.

Compile C Program In Linux With GCC (C program Compiler)

GCC is the most preferred way to compile a C program. So, In this tutorial post, we will learn how to compile our program with GCC.

GCC is the short form of GNU Compiler Collection which is a set of compilers. GCC Contains my compilers like C, C++, Ada, Fortran, Go Language, and D Language.

If you want to know more about GCC then you can visit its official site www.gcc.gnu.com.

Compiling and running a program in Linux is not a very difficult task but still, for a newbie, it could be hard to compile a C program in Linux Distribution. Well, if it is the same for you or you are a newbie then this tutorial is perfect for you.

Compile C Program in Ubuntu or Debian

I am using Ubuntu or Debian Operating System here for teaching you how to Compile a C program.

To compile a C Program on your Linux Machine you need to have the essential package installed and configured in your machine.

Step 1: Install Build Essential Package

Open the terminal of your computer and run the following command in order to install Build-Essential Package.

$ sudo apt-get install build-essential
Compile C Program in Linux

When you will run this build-essential command on your Linux machine then you will be asked for the root or administrator password of your computer.

When you Enter the Password and hit Enter Button. You will see the size of the tool and will be asked-

Do you want to continue? [Y/n]

Run C Program in Linux

If the number of memory size it consumes is available in your computer then press ‘Y’ and hit Enter. Once you hit enter, the installation process begins sooner and within a few minutes, the program will be installed into your machine.

Step 2: Write Your First C Program on Your Ubuntu Machine

In order to write a C Program, you can use Vim Text Editor, or also you can use any other graphical text editor which is available in your operating system.

Now write your C program into the editor and save it with the .c extension.

Let’s have an example-

#include<stdio.h>

int main()
{
printf("\nHello From FactForGeeks.com \n\n");
return 0;
}

This is an example of a C Program. I gonna save it as ‘hello.c’

Compile C program In Ubuntu

Step 3: Compile C Program In Linux: Compile with gcc

Compiling a C Program in Linux could be a bit different from other operating systems like Windows and MacOS but is not harder at all. It is so simple, if you learn the syntax then it is the easiest thing ever.

To compile the C program in Linux we will use GCC compiler. You don’t need to download or install GCC compiler from external sources it comes by default with the operating system itself.

Syntax-

$ gcc ProgramName.c -o ProgramName

Here in the syntax, GCC is the command and ProgramName is the Name of the program. In my case, The program’s name is ‘hello.c’.

$ gcc hello.c -o hello

Note: if the program has no error and compilation is successful then you will not get any output when you compile your C program with GCC.

So this was how you can compile with GCC.

The program is successfully compiled, Now you need to run the program for the execution.

Note: If you save the program in the Home folder of your Linux operating system then this code will work otherwise you will have to specify the complete path of the program.

Step 4: Run C Program in Linux

As you know you don’t get any results when you compile the C Program. So, In order to get the result of the program, you will have to run it.

Running a program in C on Linux Operating System is very simple and easy. Run the following command to run a C program.

Syntax:

$ ./ProgramName

In my case, the program name is hello.c. So, I need to run the following code:

$ ./hello

Note: When you run a C program then do not use the file extension. The File extension of the C program is .c (dot c).

Compile with gcc and run it

Compile and Run C program on Arch Linux or Arch Based Distributions

GCC by default comes with Ubuntu but if you are an arch Linux user you will have to install GCC manually. Installing GCC in Arch Linux is also an easy process. Let’s see how to Compile C Program using GCC on Arch Linux.

Update the Pacman database by using the following command:

$ sudo pacman -Sy

Step 1: Install Base-Devel Meta Package

Arch Linux has a base-devel meta package. In order to compile and run the C program on your Arch Machine, you will have to install the base-devel meta package.

Once you install dase-devel in your Arch Machine you will be able to compile programs in C and C++ programming.

In order to install base-devel you will have to run the following command on your machine:

$ sudo pacman -S base-devel

When you run the given command then you will be asked for the root password. Put the root password and enter.

Now you will be asked which repository you want to Install into your machine.

Find the GCC in the list and enter the number of GCC and proceed ahead.

In my case, to install GCC only I will have to put the number 10.

Within a few seconds, the GCC compiler will be installed into your system. If the compiler gets installed without any error you will see the following screen.

Step 2: Writing First C program on Arch Linux

Now you have installed a C compiler on your Linux Machine. It’s time to write your first C program, Compile and Run it.

Create Project Directory and C program’s source file-

  1. Create your project directory

    You can create your project directory or you can save it in the root directory of your computer.

    Use mkdir command to create a project directory. Example: mkdir ProjectName

  2. Now you will have to navigate into the newly created project directory.

    Run cd and the name of Project Directory to navigate into the newly created project directory. Example: cd ProjectNameCompile C Program in Linux

  3. Now you need to create a C program’s source file.

    Create a C program’s source code file using the touch command. Example: touch hello.c

    In my case, I am creating hello.c program.Create C File and Compile in Linux

By now, you have created the project directory and source file.

Now open any text editor and write your first C program on your computer. In my case, I would like to Nano (Text Editor Program).

Open hello. c in nano(In your case, The file name of the C program). hello.c is the C program’s source code file that I created for the example.

Syntax:

$ nano ProgramName.Extension 

Example:

$ nano hello.c

You will get an interface like this-

Write Your First C Program in the editor-

#include<stdio.h>

int main()
{
printf("\nHello From FactForGeeks.com \n\n");
return 0;
}

Now, Save the program-

Press Ctrl+X (Control X)

Press ‘Y’ and hit enter.

The Program will be saved now you need to Compile and Run the Program.

Step 3: Compile the C program on Arch Linux-

The syntax for compiling the C program:

$ gcc ProgramName.c -o ProgramName

In my case, I will have to run the following command-

$ gcc hello.c -o hello

Step 4: Run The Program on Arch Linux

By now, you have written the C program and Compiled it. Now it’s time to run it. Running a C program is the simplest task ever.

Syntax:

$ ./ProgramName

In my case, In order to run my hello.c program I will have to run the following code-

$ ./hello

All Done..

Conclusion:

In this post, you have learned how you can install the GCC compiler on your Linux Machine, how to write a C program on Linux, How to compile a C program on Linux, and How to Run It. This tutorial is for Ubuntu, Debian, and Arch Linux-based distributions. But to time this article will be updated and I will cover how to install GCC on any Linux Distribution and How to Compile with GCC.

Leave a Comment