C Tutorial



C Tutorial

How to compile a C program in Linux

Wondering how to compile C program in Linux?

Understanding C program compilation in Linux is an essential skill for anyone interested in C programming, whether you're a beginner or an expert. This article will provide a detailed step-by-step tutorial on how to compile a C program in a terminal, in addition to providing a C program in Linux example. We'll also discuss how to compile C++ programs in Linux and how to run a program in Ubuntu terminal.


C is a popular and powerful programming language known for its flexibility and efficiency. Many computer systems, including Linux, are written primarily in C. Knowing how to compile and run C programs is crucial in learning and using the language effectively. This article aims to demystify the process of compiling and running C programs in Linux.

Why do We Need to Install the Build-Essential Package?

The build-essential package is a collection of essential software and libraries required for building Debian packages on Debian-based systems like Ubuntu. It includes software like make, gcc and other required libraries necessary for compiling code.

While programming in C or C++, a compiler is needed to turn the human-readable code that you write into machine code that a computer can understand. The most commonly used compiler for C and C++ on Linux is GCC, short for GNU Compiler Collection. This compiler is one of the many tools included in the build-essential package.

The build-essential package includes the GCC compiler and g++ (the C++ compiler), and make. The 'make' utility helps manage and automate the compilation of programs beyond the simple programs you'd write in the beginning. As your program grows, you would split it into multiple files that need to be compiled separately and then linked together. The make utility automates this process. It's able to track which files have been changed and need to be recompiled and can perform the compilation and linking steps in the correct order.

It also includes additional utilities such as dpkg-dev which contains tools necessary for building Debian packages, and libc6-dev, the libraries and header files where standard functions (like printf, scanf, etc.) are defined.

In summary, if you plan on programming or compiling software from source code on Linux, the build-essential package will likely be a necessary addition. Without it, your system wouldn't have the necessary tools for code compilation and software creation, thus, it is an essential step in the process of setting up a C or C++ development environment on a Linux machine.

Installing the Build-Essential Package

Follow these steps to install the build-essential package on your Linux machine:

Step 1: Open your terminal.

Step 2: Update your package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories. Run the following command: sudo apt update.

Step 3: Install the build-essential package by executing this command: sudo apt install build-essential.

Step 4: After you've installed the build-essential package, you can compile C programs in your Linux environment.

Creating a Simple C Program in Ubuntu Using Text Editor

Once your Linux environment is ready for C programming, you can create your first C program. Use the text editor of your choice, such as nano or vim.

For this example, we'll use nano:

Step 1: Open your terminal.

Step 2: Type nano hello.c to create and open a new C file named myfile.c.

Step 3: In the text editor, write the following program:

#include <stdio.h>

int main() {
   printf("Hello, World!\n");
   return 0;

Step 4: Press Ctrl+O, then Enter to save the file, and Ctrl+X to exit the editor.

How to Compile C Program in Linux using the GCC compiler

To compile the C program you just wrote, use the gcc compiler:

Step 1: Open your terminal, navigate to the directory containing your C file using the cd command.

cd Documents

Step 2: Type gcc myfile.c -o hello to compile your C file. The -o hello part of the command names the output file "hello".

gcc myfile.c -o hello

Step 3: Run your program by typing ./myfile.

You should see the output Hello, World! in your terminal, indicating that your C program has successfully compiled and run.

Using Visual Studio Code Editor in Linux

Visual Studio Code (VS Code) is a popular code editor that supports C/C++ programming. It provides a rich environment for coding, debugging, and problem-solving.

Here are the steps to set up Visual Studio Code for C programming in Linux:

Step 1: Download and install Visual Studio Code from the official site.

Step 2: Launch VS Code.

Step 3: Install the C/C++ extension by Microsoft from the Extensions view (Ctrl+Shift+X).

Step 4: Open your C file with VS Code, or create a new one using the New File option.

Step 5: Write your C code in the editor.

\ Use the built-in terminal (Ctrl+`) to compile and run your C program as we described in the previous section.

Running Your First C Program in Ubuntu Terminal

After learning how to compile a C program in Linux, it's crucial to know how to run C program in Linux online. Specifically, we'll look at how to run C program in Linux Ubuntu terminal, one of the most popular Linux distributions.

1. After writing and saving your C program, open your terminal.

2. Navigate to the directory containing your C file using the cd command.

3. Compile the C program with gcc filename.c -o outputname, replacing "filename" with the name of your file and "outputname" with the name you want your output file to have.

4. Run the compiled program by typing ./outputname into the terminal.

You should see the output of your program displayed in the terminal.

Compiling and Running C++ Programs in Linux

While this guide primarily focuses on C, you might also wonder how to compile C++ program in Linux. The process is similar to compiling a C program, with the primary difference being the use of g++ (the GNU C++ compiler) instead of gcc.

1. Write your C++ program and save it with a .cpp extension.

2. Open your terminal and navigate to the directory containing your C++ file.

3. Compile the program with g++ filename.cpp -o outputname.

4. Run the compiled program by typing ./outputname into the terminal.

The output of your C++ program should now appear in the terminal.

Running C Programs in Linux Online

There might be situations where you want to compile and run C programs in Linux online without installing any software on your local machine. Several online platforms, such as, JDoodle, and others allow you to do this.

1. Navigate to your preferred online platform (e.g., or the JDoodle website (

2. Create a new C programming environment.

3. Write your C program in the provided text editor.

4. Compile and run the program using the platform's built-in tools.

You should see the output of your program displayed on the website.

Understanding C Program Output in Ubuntu Terminal

After you've learned how to compile and run a C program in Linux Ubuntu terminal, the next step is understanding the output. The printf function in C is used to output text to the terminal. For example, a printf("Hello, World!"); statement would output the text "Hello, World!" when the program is run.

Remember that the output you see in the terminal is the result of your program's execution. If your program has logic errors, the output might not be what you expect. Debugging tools and careful programming practices can help ensure your program produces the correct output.

Remember to study and understand your C program output in Linux Ubuntu terminal, as it's a critical part of learning and improving as a programmer.


Mastering how to compile a C program in Linux is a fundamental step in your programming journey. Whether you want to learn how to compile C program in terminal or use an integrated development environment like Visual Studio Code, you are enhancing your skill set and deepening your understanding of C programming on a Linux platform.

At upGrad, we offer courses tailored to make you proficient in programming, with practical, hands-on experience and mentorship from industry experts. With courses like Full Stack Software Development Bootcamp, you get expert guidance with an immersive learning experience, imparting in-demand skills to help your career become future-proof!


1. How can I run a C program in Linux online? 

Several online platforms provide Linux environments where you can compile and run C programs. Some of these include, JDoodle, and others.

2. How can I run a C program in Linux Ubuntu terminal? 

First, you need to compile the program using the gcc compiler (gcc filename.c -o outputname). Then you can run it using ./outputname.

3. Can I compile and run C++ programs in Linux? 

Yes, you can use the g++ compiler that comes with the build-essential package to compile and run C++ programs in Linux.

4. Can I write and compile C programs in Visual Studio Code on Linux? 

Yes, Visual Studio Code supports C programming on Linux. You can install the C/C++ extension to enhance the coding experience with features like Intellisense and debugging.

Leave a Reply

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