Sunday 18 March 2012

C programming on Linux For Beginners


Linux is probably the best platform to start developing programs for beginners.This tutorial covers C programming basics like installation, compiling first program, running first C program on Linux.


C Programming on Linux:
Here is step by step detailed guide on showing you how to write and compile a C program in Linux. Note that the C code that you will write on Linux will be same that you would write on Windows/DOS, as long as you are writing ANSI C code. Some library functions, such as those provided by conio.h and graphics.h, are not part of the ANSI standard. Hence you won’t be able to use them on Linux. The C compiler you use on Linux is GCC





Open a terminal and run the command gcc:


$ gcc
gcc: no input files


If you see something like the above output, gcc is already installed. If you see something like “Command not found”, then you will have to install gcc using the package manager. Besides a compiler, you will also need the C standard library, called glibc, to compile your C programs correctly. Type in

# locate glibce




and check the output. If it shows directory structures of the form ‘/usr/share/man/man7/glibc.7.gz’ or ‘foo/bar/glibc’ or the like, then you have glibc installed; else you need to install it.
Okay, now that we have confirmed the presence of a text editor, a compiler and the standard library, let us write our first code in C on Linux. For demonstration purposes, I’ll show you how to write and compile Hello World!
Start up gedit (You can use vim or other editor also) and input the simple C code to print the Hello World!

Or use the terminal to open your favourite text editor, type in
 


$ gedit PROGRAM_NAME.c   e.g  $ gedit Hello_World.c
OR
$ vim PROGRAM_NAME.c    e.g   $ vim Hello_World.c


Now input this simple C code to print Hello World!

#include <stdio.h>


int main()
{
    printf("Hello World!\n");
    return 0;
}
Save this code with the name  Hello_World.c  Now, compile the code using the following command:
$ gcc  Hello_World.c  
After executing the command, type in
ls -l  
You will see an ‘a.out’ file. This is the executable file of your C program, compiled and linked with the appropriate libraries. To execute it, run(note the leading ./, which is essential!):

$ ./a.out
Hello World!

 Congratulations, you have just written your first C program on Linux! That was just the normal C that you write on DOS or Windows – no surprises there!
A bit more about this a.out file: This is the Linux equivalent of the .exe file that you would see under DOS/Windows; it is executable form of your code. As you might have already guessed, this file cannot be executed on DOS or Windows, since it is in a different format. Now instead of having to rename your executable file each time you compile, you can specify the output file name to the compiler:
 $ gcc -o Hello_World Hello_World.c
If you still have any questions/concerns/suggestions, share it on our comment below!

No comments:

Post a Comment