C in Linux


                      Today the programming world divided into two major camps (i) The Windows world and (ii) Linux world. Linux has steadily drawn attention of programmers across the world and has successfully created a community of it's own. Without any discussion lets do a interesting journey through Linux. I hope you will enjoy it.


What is Linux:

Linux is a clone of the Unix operating system.  Its kernel was written from scratch by Linus Torvalds with assistance from a loosely-knit team of programmers across the world on Internet. It has all the features you would expect in a modern OS. Moreover, unlike Windows or Unix, Linux is available completely free of cost. The kernel of Linux is available in source code form. Anybody is free to change it to suit his requirement, with a precondition that the changed kernel can be distributed only in the source code form. Several programs, frameworks, utilities have been built around the Linux kernel.
They distribute the pre-compiled kernel, programs, utilities and frameworks on a common media. Moreover, they also provide installation scripts for easy installations of the Linux OS and applications. Some of the popular distributions are RedHat, SUSE, Caldera, Debian, Mandrake, Slackware, Ubuntu etc. Each of them contain the same kernel but may contain different application programs, libraries, frameworks, installation scripts, utilities, etc. Which one is better than the other is only a matter of taste.


C-programming under Linux:

How is C under Linux any different than C under DOS or C under Windows? Well, it is same as well as different. It is same to the extent of using language elements like data types, control instructions and the overall syntax. The usage of standard library functions is also same even though the implementation of each might be different under different OS. For example, a printf( ) would work under all OSs, but the way it is defined is likely to be different for different OSs. The programmer however doesn’t suffer because of this since he can continue to call printf( ) the same way no matter how it is implemented.
But there the similarity ends. If we are to build programs that utilize the features offered by the OS then things are bound to be different across OSs. For example, if we are to write a C program that would create a Window and display a message “hello” at the point where the user clicks the left mouse button. The architecture of this program would be very closely tied with the OS under which it is being built. This is because the mechanisms for creating a window, reporting a mouse click, handling a mouse click, displaying the message, closing the window, etc. are very closely tied with the OS for which the program is being built. In short the programming architecture (better known as programming model) for each OS is different. Hence naturally the program that achieves the same task under different OS would have to be different.

"Hello world" Programmed in Linux:

As with any new platform we would begin our journey in the Linux world by creating a ‘hello world’ program. Here is the source code....

int main( )
   { 
     printf ( "Hello World\n" ) ;
     return 0 ;
    } 

            The program is exactly same as compared to a console program under DOS/Windows. It begins with main( ) and uses printf( ) standard library function to produce its output. So what is the difference? The difference is in the way programs are typed, compiled and executed. The steps for typing, compiling and executing the program are discussed below.

How to type C in Linux:

I discuss it step by step. You have to follow it. Lets start:
             1. Open 'gedit text editor' in your system.
             2. Type the program and save it with the extension '.c'
             3. Now open your Terminal and go to that directory where you saved your programmed file.
             4. After going to that particular directory you have to use gcc compiler to compile the file.                       We shown it below. Suppose you save that file with named "Linux.c".
                 Then type as:
gcc<space>Linux.c<space>-o<space>Linux
                  after typing this hit 'Enter'.
             5. Now your file converted into binary file and has been saved as Linux in your directory,                       where you saved your '.c' file. Make sure that binary file is being executed correctly.
             6. Now for output after step 4 you have to write:
                                                                   ./Linux
                 then hit 'Enter'.

your program will be executed.

If you have any problem comment below, i will help you. 

No comments:

Post a Comment