How to Write, Compile and Run C Program in Linux

How to Write, Compile and Run C Program in Linux
How to Write, Compile and Run C Program in Linux

Write, Compile and Run C Program in Linux

Linux is a free and opensource operating system widely used for programming. While is C is a procedural programming language. You can write, compile and run C program in Linux like in Windows operating system or MacOS, etc. In this tutorial, you are going to learn how to Write, Compile and Run C Program in Linux by using the terminal.

Prerequisites

Before you start Writing, Compiling and Running C Program in Linux. You must have a non-root user account on your server with sudo privileges. Make it sure that you are connected to the internet when performing the next step.

Install Build-Essential Package

To compile and run the C program in Linux you will need to install build-essential package. To install build-essential package run following command.

For Ubuntu, Debian and Debian Based Distribution:

sudo apt-get install build-essential

For CentOS, Fedora, RHEL:

sudo yum groupinstall 'Development Tools'

Writing a C Program

Create and open a new file named demo.c by using your favorite text editor. To do so run following command to create and open a new file:

sudo nano demo.c

Paste following code in the above-opened file:

#include

int main()
{
printf("Hello World");
return 0;
}

Press Ctrl+x and then press y to save and exit the text editor.

Compile the C Program

Following is the basic syntax for compiling C program:

gcc programName.c -o programName

Now its time to compile the written C program. To compile the program run below command in the terminal:

gcc demo.c -o demo

If there is no problem you won’t get any output.

Run the C Program

Following is the basic syntax to run a C program in Linux.

./programName

To run the C program from file demo.c run following command:

./demo

The output should be:

Hello World

Conclusion

In this tutorial, you have learned How to Write, Compile and Run C Program in Linux successfully. If you have any of the queries regarding this then you can comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here