Saturday, August 9, 2008

C PROGRAMMING IN LINUX

C programming has the same beauty whether it is in Windows or Linux. But Linux provides much more possibilities for developers. The main reason being that Linux is entirely coded in C.

C programming can be done on the UNIX interface as well as on any Integrated Desktop Environments (IDE). Anjuta is a common IDE which can be used for the same. But I would recommend using the non - graphical environment just for the sheer fun of it. Here I am explaining the steps for C programming in Linux. This one too like the last post is STRICTLY FOR BEGINNERS.

Step 1:- Go to the required directory where the c program file is to be created. The UNIX commands necessary for this step would be

cd abc
- to change directory to abc
cd .. - goes to the parent directory
pwd - shows the present working directory
mkdir abc - To make a directory named abc in the present working directory
ls - lists all files and directories in the present working directory
ls -l - gives a detailed list of files and directories in the pwd
mv ab ../cd - moves the file ab to another directory cd which's in parent directory
mv abc def - renames the file abc to def
rm abc - removes the file abc

Use these commands where necessary. If you are just checking C programming, it won't matter which directory you are creating the file in. In that case avoid Step 1.

Step 2:- Create the C file. For this any editor available in Linux can be used. I use vim. The command to be used is

vim abc.c

This creates a C file in the present working directory named abc.c and takes us to the editor. Or opens the file in an editor if it is already existing. In the editor we can write the content or make any change. In some cases you might have to press the insert key before typing.

int main()
{
printf("HELLO WORLD\n");
return(0);
{

This is the "Hello World" program for C. Hello world program is the first program introduced to any student who is learning any programming language.

After typing the program press escape key. Now press the colon (:) key followed by wq if you want to write and quite. If you want to quit without saving just press :q and so on. Editor is closed now and the UNIX screen comes.

Step 3
:- Compiling the C Program. The command cc is used to compile a C program.

cc abc.c

This compiles the file abc.c and creates an executable file too. If there is no error in compiling, then no message will come. Else the error or warning messages will be displayed.

In certain Linux distro's like ubuntu etc use command gcc instead of cc.


Step 4:- Running the compiled file

The earlier step creates an executable file name a.out. To run this use this command

./a.out

The program will run now. Just remember that whatever be the name of the file, the name of executable file will always be a.out. So its better to change the name of the executable file to a unique one after it has been compiled. Otherwise after another file is compiled, this one will be replaced.

cc abc.c
mv a.out abc


This commands will rename the executable file to abc. So now the executable file can be executed as

./abc



The steps are summarised below
1. Go to the directory needed.
2. Create the C file vim abc.c
3. Compile the C file cc abc.c
4. Run the executable file ./a.out