How To Make First C Program in Linux
Nowadays programming language is getting more popular and is being used in each and every domain. Various applications, software etc. are created by programming. C programming is easy and simple language, which can prove to 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 understand the concept of Linux in brief. Most of the part of Linux kernel is written in C language. So knowledge of C programming helps to understand Kernel program and application.
Requirement of tools for C program
Three basic tools are required to create Cprogram in Linux:
- Text Editor
- GCC Compiler
- C library
Text editor is a simple application in which you can edit, structure text and use it for programming language. Text editor is similar to the notepad in Window OS.
Linux supports two types of text editor:
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)
Text editor is also useful for creating source code of C language or another languages. Various types of text editor are available for Linux. You can choose any one text editor for your Linux system.
A complier is an extended tool which converts source file to object file (machine code) so that CPU can understand it. C compiler is included in GNU Compiler Collection (GCC) which provides free software. GCC is an important component of Linux distribution. We need to install GCC C compiler in Linux for C programming.
Check GCC is installed or not in your system by following command and then press Enter key:
If gcc is installed, following result will be displayed on screen:
gcc: fatal error: no input files
If gcc is not installed, following result will be displayed on screen:
command not found
Install GCC from ubuntu software package using the following command:
sudo apt-get install gcc
GCC C compiler has inbuilt system library for different purpose like standard I/O interface, math operation etc. So we need to install C library in Linux for C programming.
Check if C library is installed or not in your system by following command and press Enter key:
Above command will display various lines on terminal screen. You can see glibc.7.gz compressed file of library.
Making Source code
I will explain simple C program for understanding concept of C programming in Linux. Let’s write the program for print “Hello World” in C which is very common and can be found anywhere.
First open the text editor which you had installed in your Linux system and write the code which is called source code.
int main ( )
Printf (“Hello World…n”);
This code should appear as same in file and save it with named hello_world.c. You can give any name to the file but it should be easy to understand. Each file requires .c extension which is an indication to the compiler that it is the source file.
Compiling the source code
After written and checked for error, next step is ready for compiler. Compiler performs linking automatically.
Compile the source code with GCC by following line:
After compilation, GCC will generate executable (runnable) file. Here we haven’t given the name of executable file so compiler automatically gives the a.out name to the file. a.out is an executable file of hello_world.c and is located in the same directory where source file is saved.
But good programmer always gives the name of an executable file using following line instead of upper method:
gcc -o Hello hello_world.c
Now GCC will generate executable file named Hello.out instead of a.out. Before running the executable file, confirm that the executable file is generated within the same directory where the source file is located.
Running the Executable File
Type the file name followed by a dot and forward slash and press the ENTER key as following:
The result will print Hello World… on the monitor screen.
Filed Under: Featured Contributions, Tutorials