How to create, compile & run a C Program in Linux terminal

The C programming language is still alive because it is simple and can do a lot of things. As we know Turbo C compiler is a discontinued integrated development environment, well, on Linux you don’t need it as there is already GNU Compiler Collection to compile and run C or C++ programs. Therefore, if you know the C language, it is much easier to learn, write programs and run other programming languages ​​on Linux operating systems such as C ++, Java, Perl, or PHP, as the languages ​​have certain similarities. Here we will show the steps to install GCC compiler and how to write, compile and run a C program in Linux.

Now, what is a program?

Let’s say you want to train your cat or puppy how to sit or jump on your commands, then what language you will use. Obviously, your mother tongue whether it is English, German, Chinese, Hindi, or something else. We will not bark or meaau. In a similar way, if we want to tell our computer to perform some specific tasks such as calculations, we need to train it how to do that, with the help of a set of rules. But the problem is Computer only understands the binary language that is 0 or 1, thus we have created Programming languages to create programs that we can understand and after compiling our computer and later can execute the same. Thus, a program is a sequence of actions to achieve a goal.

On the Page hide

Steps to write, run and compile C program in Linux

Here we are using Ubuntu 20.04 LTS, however, the steps given here are not only for it. You can also implement on older Ubuntu versions such as 18.04/16.04 including Linux Mint, Debian, Kali, CentOS, RedHat, Fedora Elementary, and more…

1. Install Compiler and other Dev tools

To write and execute a C program on Linux, we need a compiler that will compile the code we have written and give us an executable file for the same. Therefore, for that, if you are on Debian or Ubuntu then install build-essential, and on RHEL based distros go for Development Tools.

For RHEL/Fedora/CentOS

Run system update command, first:

yum update
dnf groupinstall 'Development Tools' or yum groupinstall 'Development Tools'

On Ubuntu or Debian systems

sudo apt-get update sudo apt-get install build-essential manpages-dev

You should have sudo or root user access to run the above commands…

Install development tools on Ubuntu and Redhat centos

2. Check GCC version

GCC is the open-source library, which is an acronym for GNU Compiler Collection available for Linux and acts as a compiling system for Program C and other various programming languages. However, it mainly used to compile C and C++ programs… Thus, after installing the development tools in the first steps, you will also get GCC on your system. To confirm and check its version run:

To get full info:

gcc -v

To know only installed path

whereis gcc

For version, only-

gcc --version

Check GCC compiler version

3. Open a Text editor on Ubuntu or RHEL

All Linux distros now come with graphical text editors, however, we can use the command terminal to create a text file to write C program codes as well using command line text editors such as nano, VIM, gedit, and more... Here we are using nano.

To install nano, if it is not on your system type:

RHEL/CentOS- sudo yum install nano

Ubuntu/Debian– sudo apt install nano

4. Write your first C Program in Linux terminal

Let’s create a demo C program in which we will include common C program library stdio.h to use various variables including functions for performing input and output. If you are learning C programming then you already familiar with the C libraries that we define in the header of the program to call various functions for performing various tasks.

For example, if you are writing a C program that mathematical functions then we need to declare math.h library, for graphics, we include graphics.h and so on…

Create a file:

nano demo.c

Now, let’s add the following lines to create a simple C program that will give an output “Say hello to H2s” when we compile and execute it. To Save the file by pressing Ctrl+X, type Y, and then hit the Enter Key.

// my first demo C program #include int main()

The explanation for the above command- In the above command first we have added stdio.h library in the header and then int main(); in this syntax main() is the entry point of any C/C++ program, in short, it tells the compiler to start compiling from this point and int main() function will accept any number of arguments but it returns integer value usually zero. However, that is an old way, developers usually prefer int main(void) this represents a function that expects no arguments or when a program doesn’t require any initial parameters.

After that we use printf function to show or print a text ” Say hello to H2S” and to break the output line or to have a page break, we used \n (Escape Sequences). As we have used the main () function that generally supposed to return 0 value to confirm there is no failure in compiling and the task has been finished successfully, however, if it gets non-zero that means a failure. Nevertheless, declaring return 0; is not compulsory even without the program and the main () function will work in the same way.