How to Write, Compile and Run C Program in Linux

Table of Contents
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.
LATEST POSTS
-
How to Install PhpStorm on Linux Mint 19
-
How to Install Docker Compose on Debian 9
-
How to Install Dropbox on Manjaro 18
-
How to Install CMake on Linux Mint 19
-
How to Install Anaconda on Debian 9
-
How to Install Apache Tomcat 9 on Ubuntu 18.04
-
How to Install Minecraft on Linux Mint 19
-
How to Install Go on Linux Mint 19
-
How to Install PHP 7.2 on Debian 10
-
How to Install Redis on Debian 9
-
How to Install Kodi on Ubuntu 18.04
-
How to Install IntelliJ IDEA IDE on Ubuntu 18.04