Thursday, December 29, 2011

The C beginings: part 2

In the last part(i.e part 1) i said something about the origin of C, what C language is about and what it is generally used for. In this post i shall go a step further by giving you an example and explaining every line in details....
In most tutorials, they start by showing you how to display " Hello World " , this is always a good place to start but i will try to run through the C language so that you understand it in great details in the shortest possible time after all you are learning at Gigabyte speed.... So here go...
First i shall explain what happens when a C program/code is complied (i am assuming you know what a compiler is, if you do not; click here for a quick run through )
A program in C language is written by first including a library file that should contain all the functions you will be working with. This includes the particular file that the compiler needs to check in order to know what each of the functions you use stands for. Like in a library where each book holds or contains a particular information that you will need to enable you answer/solve a particular problem set, also for programming you have to tell the compiler which file contains the information it will need to solve a particular task. Below is a sample code that displays the words " Hello World " without the quotes on the command line. For a run through on batch programming in MS-DOS click here.

#include <stdio.h>

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

I will start explaining the above code line by line. Note the C compiler is not sensitive to spaces in between the wordings, i have spaced the above code in this manner for clarity and it is also a good programming technique to make your code readable by other programmers or by yourself when you go through the code later.
The first line contains the #include <stdio.h>   first notice the word in-between the < > brackets. It is stdio.h and it means standard (std) - input(i) - output(o) - .header file(.h)  . This is the default file in the C standard library that contains functions and methods of interacting with the input and output devices of the machine(could be a computer-PC, micro controller, etc.) you run the code on. The # tells the pre-compiler that this line of code is meant for it, so the code is telling the pre-compiler to go and fetch the standard input output file and include it in the code. The < > bracket mean the the pre-compiler should search all the memory of the computer for that file, in order to specify to the pre-compiler that it should search only the current directory where the file containing your code is stored, you use double quotes(" ") .
The second line starts with int main( )  this tells the compiler that this is the main block where the critical or important code to run is. The int in front of the main( ) is not really necessary because by default all C main methods return an integer value (int for short).
The next line of code tells the compiler that starting from the {  to the } contains a block of code grouped under int main( )
Within the { } contain the code printf(" Hello World ");  meaning print formatted data Hello World to the default output device(in this case the command line screen). The semi colon at the end tells the compiler that it has gotten to the end of a statement. The return 0 on the next line just returns an integer of zero to indicate to the compiler that the code executed properly.
By this stage, i know you have  a lot of questions, feel free to leave them as comments below this post and i will answer it as soon as possible. If you wish to contact me for any reason use the form on the contact page of this blog.



Season's Greetings from Giga Frequency

No comments:

Post a Comment