How to make your first C program on Linux (Part 3/15)

How to make your first C program on Linux

Nowadays, programming language is becoming more popular and being used in every domain. Various applications, software, etc. are created by programming. C programming is an easy and simple language, which can be a useful choice for a beginner who wants to become a programmer. C language is the basis of all languages ​​and is useful to briefly understand the concept of Linux. Most of the Linux kernel is written in C language. Therefore, knowledge of C programming helps to understand the Kernel program and application.

Tools Requirement for C Program

Three basic tools are needed to create Cprogram on Linux:

  • Text editor
  • GCC Compiler
  • C Library

Text editor

Text editor is a simple application in which you can edit, structure text and use it for programming language. The text editor is similar to Notepad on Windows OS.

Linux supports two types of text editors:

1) Console text editor – used in command line environment (i.e. VI, vim, nano)

2) Graphical text editor – used in graphical environment (i.e. gedit, gvim, NEdit)

The text editor is also useful for creating source code for the C language or other languages. Several types of text editors are available for Linux. You can choose any text editor for your Linux system.

GCC Compiler

A compiler is an extended tool that converts the source file into an object file (machine code) so that the CPU can understand it. The C compiler is included in the GNU Compiler Collection (GCC), which provides free software. GCC is an important component of the Linux distribution. We need to install GCC C compiler on Linux for C programming.

Check whether GCC is installed or not on your system by following the command and press the Enter key:

gcc

If gcc is installed, the following result will be displayed on the screen:

gcc: fatal error: no input file

compilation terminated.

If gcc is not installed, the following result will be displayed on the screen:

command not found

Install GCC from the Ubuntu software package using the following command:

sudo apt-get install gcc

C Library

GCC C compiler has a built-in system library for different purposes such as standard I/O interface, mathematical operation, etc. So we need to install C library on Linux for C programming.

Check whether the C library is installed on your system or not by following the command and press the Enter key:

find glibc

The above command will display multiple lines on the terminal screen. You can see the library glibc.7.gz compressed file.

Making source code

I will explain a simple C program to understand the concept of C programming in Linux. Let's write the program to print “Hello World” in C which is very common and can be found anywhere.

First open the text editor that you have installed on your Linux system and write the code called source code.

#include

int main ( )

{

Printf (“Hello world…n”);

return 0;

}

This code must appear the same in the file and save it with the name hello_world.c. You can give the file any name, but it must be easy to understand. Each file requires the .c extension, which is an indication to the compiler that it is the source file.

Compiling the source code

Once written and checked for errors, the next step is ready for the compiler. The compiler performs the linking automatically.

Compile the source code with GCC following the line:

gcc hello_world.c

After compilation, GCC will generate an executable (executable) file. Here we do not provide the name of the executable file, so the compiler automatically gives the name a.out to the file. a.out is an executable file of hello_world.c and is located in the same directory where the source file was saved.

But a good programmer always gives the name of an executable file using the following line instead of the superior method:

gcc -o Hello hello_world.c

Now GCC will generate an executable file called Hello.out instead of a.out. Before running the executable file, confirm that the executable file was generated in the same directory where the source file is located.

Running the executable file

Type the file name followed by a period and a slash and press the ENTER key as follows:

./Hello

The result will be printed Hello World… on the monitor screen.

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.