How to create, compile & run a C Program in Linux terminal – Linux Shout – H2S Media

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.

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/Debiansudo 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<stdio.h> int main()
{
printf("Say hello to H2s\n");
return 0;
}

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.

Write C Program Linux Ubuntu

4. Compile with GCC

Now, let’s Compile our first C program, we have written. We use GCC compiler

gcc demo.c -o demo

In the above command, we are compiling the demo.c file or program using GCC and saving it in an executable format that is a demo. You can save it with some other name as well.

5. Run C program in Ubuntu Terminal

Let’s Run it. For that simply type:

./output-program-name

In the above, example, we have compiled a C program demo.c and saved it an executable called demo. Thus, to run the same, we type:

./demo

Output:

Compile C Program on Linux using GCC Compiler

6. C program to add numbers in Linux

Let’s go a little step further and write a simple program in C language to add numbers. In it, we will ask to users add two numbers and the program perform the addition and print the result.

Create a new program, let’s say sum.

nano sum.c

Add the following code in that and save the file using Ctrl+X, type Y, and then press Enter key.

#include <stdio.h>
int main() { int num1, num2, sum; printf("Enter two integers: ");
scanf("%i %i", &num1, &num2); // calculating sum
sum = number1 + number2; printf("%i + %i = %i", num1, num2, sum);
return 0;
}

Program C two number add program on Ubuntu Linux

The explanation for the above code:

With the help int which is an integer variable, we are declaring three variables num1, num2, and sum. The num1 and num2 variables will hold the two numbers that a user will enter to get a result that will store in the sum variable.

int num1, num2, sum;

After that, we are printing a text to ask a user to enter two numbers that you want to add.

printf("Enter two integers: ");

Then we will use scanf function to take inputs from the user in integer format using another variable %d or % i.  %d specifies the type of variable as decimal and %i specifies the type as an integer.

scanf("%i %i", &num1, &num2);

Now, we will use + operator to add num1 and num2 and store the result of the same in sum variable:

sum = number1 + number2;

Finally, print the result of the addition using printf function. Now, what happening here is, first %i read the value stored in num1 and second %i read the value stored in num2, and third %i will read the value stored in sum. \n is just to page break line.  After that, it prints all of them together which appears to the frontend user a familiar way to get an answer to its addition query.

printf("%i + %i = %i \n", num1, num2, sum);

Compile and run your code

gcc sum.c -o sum ./sum

C programming on Linux terminal using GCC compiler

Closing thoughts:

Well, this tutorial is just an introduction on how to start with the C program in Linux, however, to go further in deep you have to acquire more knowledge through books on how libraries, functions, variables, and operators work in it.

Leave a Reply

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

www.000webhost.com