Linux is the best platform to learn C programming because it is an open source platform and you will find so many documentation on the internet if you need help.
The second reason is that it provides built-in C compiler to start running a C program.
In the series of guides, till now I have explained C programming basics with a small Hello World program.
Although there are many IDEs you can use but in this guide I am going to show you how to compile and run a C program using a Linux Terminal.
Required Setup
You can run a C program on a Linux machine in one of the below ways:
- Using a Linux machine
- By running Linux on Windows (if you have a Windows PC)
- From an online Linux Terminal
- On an online C Compiler
Choose how you want to run your C development setup to proceed.
To keep it simple and relevant for most beginners, I will use Ubuntu installed via Oracle Virtualbox on a Windows PC.
House Keeping Before Starting
Before creating and running a C program, let’s do some management work which will help us find all the C programs in one place.
I would recommend you to create a new directory where you keep all the C programs.
To do that, go to your home directory on Ubuntu and create a directory using the mkdir command, let’s say in the name of c-programming.
- First open the terminal window on Ubuntu (or Linux in general) using the below keyboard combinations:
Ctrl + Alt + TNOTE:
You can also use the respective menu items or a search box if provided in the Linux distro you are using. - Then execute the below commands on the terminal to create the directory:
cd ~ mkdir c-programming
- You can also use the ls command to verify if the directory is created or not:
ls -l
- Once the directory is created, let’s change our working directory to the new one. You can use the cd command to change the directory and then the pwd command to verify the present working directory in Linux.
cd c-programming pwd
Once you have created and moved inside the directory let’s create compile and run our first C program on the Linux Terminal.
The C Program to Compile and Run on Linux Terminal
For the sake of simplicity I will use the first Hello program that I wrote earlier in C.
Create the hello.c file
- First create a file in VIM editor in the name of hello.c using the below command:
sudo apt-get install vim vim hello.c
NOTE:
The above command first installs the VIM editor and then creates the file. - Once created the file and you are in the VIM editor screen, press the I button on your keyboard to run VIM in writing mode.
- While in the writing mode, copy and paste the below Hello World C program code in the hello.c file in VIM:
/* * This program is demo code from https://cyanogenmods.org * Author: https://cyanogenmods.org */ #include <stdio.h> /* The entry point function in C */ int main () { /* The actual print statement */ printf ( "Hello, World!" ); return 0; }
- Once copied the code, press Esc to move out of editing mode.
Then :wq! and then press Enter to exit
Compile the hello.c file on the Terminal
- Execute the below command to compile the C file using GCC compiler:
sudo apt-get install gcc gcc hello.c
NOTE:
The above command first installs the GCC compiler and then compiles the hello.c file. - After successful compilation, the compiler will generate an a.out executable file.
- You can use the
ls -la
command to see the binary file is generated or not.
Run the output binary file in Linux Terminal
- Once the output binary file is created, execute the below command to run the C program that we created:
./a.out
- The output:
Hello, World!
That’s how you need to use the Linux terminal to compile and run a C program.
I hope that was quite straight forward and simple for you to understand. If you still have doubts on something, please leave a comment and I will try to address your concern(s) as soon as possible.
For more detailed step-by-step C programming and other Linux guides, keep visiting Explore Linux.