You are on page 1of 4

C Programming in GNU Linux

- by Tridip Neogi
Scottish Church College.

Introduction:
Well, this is just a tutorial for those who are new to the GNU Linux environment and are wondering how to
program in C in the GNU Linux environment. Please don't be mistaken by the thought that this is a C
tutorial. There are quite a lot of books available for that. You can try any of that but I suggest C : The
Complete Reference by Herbert Schildt for the beginners.

This tutorial teaches you only how to write your C code using an editor and compile it using a compiler, and
to run it as well. Those familiar with programming in the Turbo C++ environment might find this a bit
strange but later you will get used to it.

Setting up the environment:


For our purpose we shall need the following tools – an editor for writing your code, a compiler to compile
the code and a shell to execute your program.

But obviously before that you need a GNU Linux platform. There are various distributions available in the
net. You can download any distro of your own and use it. However, those new to the GNU Linux
environment should choose a distro which has a good support in the net (like a big forum) or somebody to
help you out if you are stuck in the middle. In my opinion, you can start with any one of the following distros
:-
fedora
openSuse
Mandrake
Ubuntu
Mint
All these distributions are available free over the net. Simply download the image file and install it. Please
note that other distributions do exist, but those listed here are user friendly and easy to use. However, there
no such restrictions, you are free to use any. Infact you should try others when you familiar with the GNU
Linux environment. Slackware is the oldest GNU Linux distribution still available. It is very stable and if
you want to learn GNU Linux you should go for it.

Now that you have installed GNU Linux in your system, we can now start our job. Among the packages you
should install the development package before trying to program in C. That will install gcc in your machine.
The gcc package contains the compiler for our C language and will help us compiling a C source codes.

Please note that in many distributions these packages are installed by default. If you are not sure whether gcc
is installed or not, please go to a console and type the following:

 $which gcc

This shall print the location of the package mentioned if it is installed. If you are sure that you have gcc
installed and yet it is not showing then you might have to set the PATH variable.

So, we are ready with a compiler. Now we need a text editor to write our codes. There is a huge collection of
editors in the GNU Linux platform. Obviously, you can choose any. Vi editor is the one you would get in
built with any GNU Linux Distro. It is a good editor. For using it get into a console and type:
$vi
or
$vim
that would open up the vi editor for you. A GUI version of that editor is also available. You can find it in the
start menu or else from a console simply type
$gvim
Actually vim is the improved version of vi. Please read the manual of the vi editor before using.
I, however, personally prefer Kwrite (if you are using X Windows). It is very user friendly and much like
any other editors in the windows environment. For beginners I would suggest this editor. Then as you gain
experience you can move onto any such advanced editors. Emacs is also a good option but then please read
the manual before using it.

Now you are completely ready to start with C programming, but just a few notes before starting. Although it
is a common practice of the student to save their files in the home directory itself, I suggest creating a new
directory for that. This shall help you manage the files properly when their number grow heavy. You can
create a file using mkdir command.
 $mkdir Programs
  $cd Programs
Here Programs is the name of the directory that you have created. After creating it you have to move to that
directory, which you do by typing the cd command.

Here is a list of things that you should also start practicing along with programming. Most of the students
ignore these facts. Firstly you should clearly indent your code. Indentation is followed in almost all the books
available. You can use any indentation style but the C style of indentation is recommended for coding in C.
Don't worry if you don't know it. Almost all the C programming books follows this style. However, if are
using kwrite you can perform the following steps before coding:
open kwrite then 
Settings >configure editor >editing >Identantaion.
In the default indentation mode change it to C style. (This procedure may 
be different in your version.)
In gvim there is automatic indentation. However you can type the following to attend it:
$set cin!
Another important thing that most of the students often ignore is proper commenting. Please comment your
code clearly so that others can understand your code.
Please don't write the whole program in a single file. Rather create different files for different work and try
using it in your code. This provides modularity to your code. If you don't understand this don't be upset. You
will get to know it slowly.

So let's start with our work.

Beginning C Programming:

The very first program we would write is the “Hello World!” program (i.e, to print the sentence “Hello
World!” on the screen).

Open a console and go to your desired location where you are supposed to write the file (in our context we
have created the directory named “Program” to store our files). If you are not sure of it simply type:
$pwd
This shall echo the current directory you are working in. If that is not the required directory you can always
change it by the cd command. For example, when you first open the console after log into the system you
will be probably in your home directory. In that case if you want to go to the desired directory (“Program” in
our case) type the following:
$cd Program
next if you type:
 $pwd
it shall show you the current directory. Please make it sure that before using the cd command the directory
should have been created.

Once you are in your desired directory type the following:


 $vi hello.c
this shall open up the vi editor within the terminal itself. Now press 'i' within the editor and you are ready to
write out your code. Pressing 'i' lets you edit the file which otherwise you won't be able to do. For sake of
simplicity we shall write the code to print the line “Hello World!” on the screen.
#include <stdio.h>
  main()
  {
printf(“Hello World! \n”);
return 0;
  }
Once you have finished writing this first press the ESC key, then type the following:
   :x
This shall save the content in the file and exit from the editor. So, you are now back again to the prompt.
We have to compile our C code now. As mentioned above that GCC(GNU Compiler Collection) is the
compiler we are going to use over here. For compiling a C code the command is cc or gcc. The syntax for
the command is shown below:
$gcc hello.c
This shall compile the C source code and create an executable file named a.out. Note that a.out is a file
created by default. If you want to run the program just created then type the following:
$./a.out
You shall have the following output on the screen.
Hello World!
If, instead of a.out you want something else, you can explicitly mention the name with the ­o option
during compilation. The syntax looks like this:
$gcc hello.c ­o hello
This will create an executable file named hello instead of a.out. Now type
 $./hello
to run your program.

For kwrite users - simply open the editor, type your code and save it with the .c extension. Next compile
and run it as shown in the previous section.

For Turbo C++ users – you basically do the same thing here as you do there. First you write your code, save
it, then compile and run it. You do the same thing here in a different fashion. In Turbo C++ the whole thing
is embedded in an IDE while you do it manually over here.

So, you have learnt how to code in C in GNU Linux. Is it too tough??? I hope not. Try experimenting with
various stuffs.....you will get to know so many other stuffs.

Some Important Points:


Following are few things that you should know:
• As a beginner you can choose kwrite editor but then with time you must switch over to vi or
emacs or any console based editor. There are quite a few reasons for using them. Firstly they are
more powerful than kwrite, though you might not find their powerfulness in the beginning. You
will come to know about it in due time. Secondly kwrite is not available for every desktop system.
Such an editor requires you to work on X window system which might not be available to you at
certain circumstances. You are however sure to get a console based editor in any environment. So it's
always better to practice on such editors (please read the manual before using any such utilities).
• The .c extension is not required while you create a C source file. However this helps your editor to
identify the file as a C source file. Generally the vi editor (and most of the editors) provides
automatic indentation if has identified the file to be a C source file. You will find the data types are
highlighted with various colors. One of the most powerful feature is that when place the cursor on a
brace the corresponding opening or closing bracket is highlighted. If instead you find no automatic
indentation and white text with no highlight then you might probably have forgotten to provide the
.c extension. Please note that if instead of .c extension some other extension (like .txt) is given
then the file will not compile at all.
• For Turbo C users, you won't get the headers dos.h and conio.h. You might be thinking that this
might be a significant disadvantage of the GNU Linux platform, however, this is not the case.
Actually in GNU Linux interrupts are handled in a different way than they are in the Windows
platform. At this point you should also know that what you can do in Windows can also be done in
the GNU Linux platform, and even something more. All you need to do is to explore this world of
GNU Linux. You must be wondering that functions like clrscr(), which is defined in conio.h,
will not be available over here. That's right! However there is a way out. You can clear the screen
using the system call system(“clear”) for which you need to include the header stdio.h.
Another most important function which you have almost used in most of the programs in Tubo C is
getch(). Just know that you simply don't require that function to see the output.
• You won't have problem with most of the in built functions. But often you have to compile the file
providing the the compiler the necessary library. For instance if you want to use a mathematical
function in your code and you have included the math.h header for that, you have to pass the
information to the compiler while compiling the source. For math library just compile the source
code with ­lm option. In our example if we have used the math.h header then we should have
compiled our code using the following command.
$gcc hello.c ­o hello ­lm
this will link the math library with our code. For using the ncurses library you need to compile it
with ­lncurses option.

References and Websites:


• beginning GNU Linux Programming-Neil Mathew & Richard Stones 3rd edition(WROX
publication).
• http://gcc.gnu.org/onlinedocs/ - you can find documentation of gcc over here.
• http://www.vim.org/docs.php -you can download a copy of the vi documentation from here.

Every effort has been made to make this tutorial complete and simple. All the basic issues required to
program in C in a GNU Linux platform has been covered in this tutorial. If however you have some problem,
please feel free to to contact me. Also if you require some more information to be included in this tutorial
you can always contact me.
email: greenvalley74@yahoo.com

Have a Happy Journey!!!!!!!!!!!!

You might also like