6 Mar 2013

C Tutorials 1

Guys here we going to publish a easy and effective series of C tutorial which will you in better understanding of C language.
We Welcome your feedback and suggestions!!! Please Comment!!

 Tutorial-1

Any C program will contain a function called as:
 int main(int argc,char*argv[])
Now let us break this in parts and analyze one by one.

1)What is a function?
Function is a small part of entire program,containing some set of instructions that is generally dedicated to perform a specific task.
Advantages: Easy to debug,code resuability.
Disadvantage: Lengthy code.

2)
 int: it is a built in data type stands for Integer, we are going to understand the data-types in more detail in comming tutorials, as of now just understand that a data-type is something that signifies the type of data,which helps the compiler to allocate the memory. We are also going to learn about memory allocation and other stuffs in later tutorials,however as of now we can describe memory allocation similar to reserving space for something. Typing 'int' in front of the function,tells the compiler to return an Integer value once you finish the execution from the function.

3)main: This is the name of the function. Every program,like in real life, needs a starting point. main exactly does that. Generally, any name followed by '()' signifies that it is a function. This is basically syntax. Whatever is typed inside the '()' are called as parameters.

4) As of now, we can skip what is the meaning of
 (int argc,char*argv[]). We will require this when we read some file from the command line, but as of now its ok even if we skip it.

Now that we know meaning of individual parts in the main function, there are few more things that are required to understand before we can start writing our first program.

Let me explain the next concept with an example.

Consider a large thermo-flask of coffee , similarly one that of milk and so on and these flask are never empty.
 
In order to make one cup of coffee, I just have to pour some coffee and milk in my cup. That is it.
Similarly in order to make 10 cups of coffee,I just have to pour some coffee and milk in 10 cups according to their needs,they can have black coffee aswell.
The point is that I will never make coffee again by myself, all I will do is to use the coffee which is already available,use some milk which is already available,without bothering about it or how that coffee was brewed or any details on milk.

In C, there are various inbuilt functions which can be used by programers. Say for example, there is a function which helps to print some thing on screen,then there is another which helps to read the input from the user, without even bothering about it. This is called as code reusability.
In C, there are two such important files
 

1)stdio.h
2)stdlib.h


stdio.h stands for 'standard input-output header file'
stdlib.h stands for 'standard library functions header file'

What is a header file?
Header files allow programmers to separate certain elements of a code into reusable files. Header files commonly contain declarations and in this way, it helps to standardized a program.Header files donot contain implementations.

Now the question arises: how do we use the material from the above two header files? How can we convey the compiler to include these two files in our program? There is our next syntax,
 

5)#include:
 This is basically called as preprocessor directive, as from the name, we can easily guess that it is going to be accessed before the process starts. 

In C-style syntax, we tell compiler as follows:
#include
 
#include

In simple terms, compiler includes the files in the angle bracket along with your source code.Now we are in a position to write our first program based on our basic structure using some more things along with what we have already learnt.
Code:
#include
#include

int main(int argc, char *argv[])
{
  printf("Hello World!");//Print something.
  system("PAUSE");   
  return 0;
}
6)printf(): It is a built in function which is used to print the data on the screen.It stands for print formatted as it accepts a string,another data-type along with other format specifier, things will be cleared once we start to program regularly.

7)system("PAUSE"): This is pretty much self explanatory, try running your code without this and check for the results.

8)return 0: This is called as fullfilling the promise. We made a promise to the compiler that main function will return an integer once it about to exit.

Minor details before I conclude with:
 

9)'{..}' or use of curly brackets:
This tells the compiler- "No matter what it appears, treat this block of code as a single statement"
We shall see much more use of curly braces in control-structures.

10)
 ';' or use of semi-colon: this conveys the compiler that it is the end of the statement syntactically, its similar to period in english.

11)The comments: There are 2 types of comments in C.

11.a)
 // or Single line comment : This tells the compiler,"Donot consider the data beyond the '//' on that particular line" 

11.b)/* ... Some comment ...*/
 or multiline comment: This tells the compiler" Donot consider the block of data included between '/*' and '*/'

Comments are really very useful in lengthy programs, they help to make other users quickly understand the block of code or nature of variables or in general anything that the author of the program wants to convey.

Exercises:
Modify the above program,check what errors you get if you remove ';'
Modify the above program,check what errors you get if you remove '{' or '}'
Modify the above program,check what errors you get if you remove 'system("PAUSE")'
Play with it until you get bored of it.;)

No comments:

Post a Comment











































































































Support From Kuldeep Sheoran