Monday, 8 August 2011

Beginning the Programming Era !



C Basics for Beginners





Dear Friends,

How many of you just fear from the name of programming ?  How many of you just think that you are not at all born for programming? How many of you are from Non-Computer Science Background and just think that C,C++ ,etc are not your cake?
If your answer is Yes for any of these questions, then get up guys this blog is for you!
If your answer is No and You are a geek, please excuse;)

Guys , I have a personal reason for creating this blog.Three days before, My sister called me.She is doing her B.tech Biotechnology Final year and she told me one company is coming to her campus, and they will ask some Basic C , so she is very afraid and she has no hope. Well guys , I felt very helpless that time. Why?

Oh Sorry Guys I didn't introduce myself yet.I am a Btech CSE graduate , programming is my passion as well as life and I am working in a Leading Product based Company ( KLA-Tencor) as a S/w Engineer.Well , back to the point , For my sister misery , i thought of finding some useful website for her so that even a child can learn C. But unfortunately , I didn't find any useful website that can teach C that even a beginner can learn.

Guys this blog is for people who fear from the jargons of C or any programming . This will teach you C as a primary school teaching and trust me , You will never be afraid of any programming and you will start loving it.
 This blog is mainly targeted for non-CSE background people who are afraid of programming.This will teach very basics and Comments and suggestions are most welcome. Geeks and Programmers , Please co-operate I know you know well , but this blog is not for you :P

Well I have made you people so bored by my stories, let me start with.Guys first promise, you people first think God has made people and people  have made computers .So make computer as your slave, dn't be the vice versa.I will teach you full basics, concentrate properly and have fun.

What is computer? Silly question :P Ans: Computer is a programmed electronic machine.Next Question: What do you mean by programmed or programming? Ans : Any Program is a set of instructions given to the computer to execute.Now guys start concentrating on my words. First think , at the very low level , how a computer works.Nothing but there are electronic switches and gates.Switch You know it may be on or off.Guys concentrate, on or off. Thats it! that is computer.Means , comes binary(0 or 1).Clever!

So guys as you know earlier days we had machine programming in 0s and 1s , that was nothing but an instruction to the computer to switch off or on certain circuits.

So far OK? Ok guys take a breathe! Next scientists thought writing programs in 0s and 1s is very hectic as well as difficult to maintain.So they come up with the language which should be natural.Guys mark the word natural.When i say natural , I mean it should be English-Like and Understandable.

Note: People, as you know , Machine Language is called Low Level language and C,C++ etc are the high level languages .

Well , to start with C, Guys first let me tell you something about memory and CPU and program execution.
Guys whenever you save any program e.g.MyFirstProgram.c , you well know it goes into your hard disk.Fine, when you compile your program by pressing Alt-F9(For Ex in TC) , Did you ever go to the directory and see what file get generated there? The compiler converts your .c file into .o or .obj.Guys, don't be afraid.Remember, computer is your slave.Just get rid of all the jargon's what i am speaking about. You know well Computer knows only binaries.So Compiler is our friend ! What it does it , it converts our .c program into 0s and 1s combination's and save it with .o or .obj extension.Thats all !

Well still I didn't execute my program :(. What do you mean by execute? Ans: Load the program into memory and run. Your Question : What crap you are talking about , my program is already into memory. Well people, wait wait i will explain! There is something called RAM(Random Access Memory) in your system , besides your hard disk , and program need to be loaded there. ( Be clear , .obj's will be loaded into RAM ok , not the .c files . For Geeks: please excuse me , I am not telling the beginners about the .exe's , linker and all now, they will get confused).Well boys, when you press Ctrl-F9(In TC for ex) to execute , what actually happens is the binary file gets loaded into RAM and our friend CPU executes it and we see the output.

So, people I feel now that atleast you people understand now how a program gets executed and it is not a magic!

Well now i am going into the language, and sorry people i am not going to tell you about it's history.I am going to tell you about how to write a program.

Guys whenever you learn any programming language. you hear the header files word first.What it is?
I have a very good analogy of it with the real world.Guys when you are born, if that time somebody tells you get me a Diary milk. Oh the little baby, "there is no response" :D. But after ten years , if I say ,then you will bring right( provided you have money and you like to spend it for me :P) . Why? because now you know the meaning or definition  of Diary Milk and it is stored in your memory.Similarly ,dude computer is a box , it doesn't know anything(You agree right!) . All the definitions or meanings are defined in the header files.For example , when you say printf("Hello");  , the computer comes to know about it by looking into the header file "stdio.h".Just type a program without using #include<stdio.h> and use printf, compile it , your compiler will give you error "Function printf should have a prototype".So looking into the header file , the computer will come to know Oh printf means print this to the output screen.Oh guys ! So now you you believe computers are dumb and you need to tell it everything. Poor Computers!

Well , you guys would be thinking whenever I learn C from any website, the first teach me how to print hello world :P. Why this guy is not printing yet? Ok your wait is over , I will write the program , but before that some basic notes for you:
  • Any C program you write , the compiler will look for main() function first.So never forget to write main() in any program.It is the point at which program execution begins.
  • Any C statement is terminated by a semi-colon.
  • Get rid of the jargon printf(). Whatever you write in double qoutes in printf, same to same will be printed into the output screen.In Hindi I say to my students " printf k andar mai double qoutes mai jo likhoge ,jyun ka tyun output mai print hoga". e.g printf("Hello world"); will print Hello world on the output screen.
Ok guys , your first program is ready.
      #include<stdio.h>
      void main()
      {
             printf("C is not a Ghost");
      }
  Output : C is not a Ghost

Hurray! So simple, and you wrote your first C program.Just compile your code and execute it , and check for the files getting generated( What i explained above).

Question : I am not happy :(. You took this much time of mine and taught me just how to print something.I want to do something mathematical. Answer : Cool Buddy ! I am coming.

Well to answer my dear friend's answer, I will teach you something more exciting now. Guys you would have seen this statement written by your great professors in your degree colleges many times like int a; or something.
Ok we will go into this.Dude reminder ! Computer is a box , it doesn't know anything.So if you have to use an integer(hope you know what is an integer) in your program , you need to tell the box.This telling is called declaration.So when you say int a, computer understands oh you are going to use an integer and the name of the baby is a. What internally computer does it , it allocates   2 bytes (16 bits ,depends upon system) of memory to it .Note it down , the size of an integer is generally 2 bytes and it varies from system to system.
Keywords and Identifiers: he he:) You usually get this question for 2 marks in you college right ! If somebody asks me , I will tell him "Things which computer know itself by default and i needn't explicitly tell him in a program are keywords and Identifiers are my babies, what i tell(declare) to the computer before using it".e.g int is a keyword and a is an identifier.
Well back to the point,so now you need to use the baby , you will want to assign some value , for that purpose we us the assignment operator = .people don't confuse it with Maths , in computer = means "put the right hand side value into left hand side variable".
So my yet another program using this:
    #include<stdio.h>
    void main()
    {
          int a;                     //Declaration
          a=5;                      //Assignment
          printf("%d",a);
   }
Output : 5
Question: What the hell is this %d? Answer: Cool down buddy! I will tell you .See %d is just a placeholder, it tells the computer that at the run time , replace the %d by the integer value given after comma(here,a).Technically , these are format specifiers and you will come to know there is %d for integers,%f for floating point numbers,etc.

So one tricky question, If i say printf("a"); what it will print. If you say 5 , get out of my class.If you say a, I am happy be here.Remember, Whatever you write in double qoutes in printf, same it will be printed, only if some format specifiers are there, the actual values will be put there. Ok?

So, now you can make your output more fancy like:

printf("The value of a is %d",a);

Output:The value of a is 5.

So far everthing is so simple.Lets do something like adding two numbers.A Mantra here:

Ankitmantra1: Before writing any program , just imagine and think the number of variables you need and declare it , life will be easy.

So for adding two numbers , I need how many variables? Three! Because the two numbers I will add ,but i need to put the sum somewhere right,So i need a third variable.

So the code is:

#include<stdio.h>
void main()
{
int a,b,c;
a=2;
b=3;
c=a+b;
printf("the sum of %d and %d is %d",a,b,c);
}
Output: The sum of 2 and 3 is 5.
Hurray!Dude now you are ready to do any any mathematical operation and so easily.Add,subtract,mul,div,AP,GP,Simple Interest,etc. Please be enthusiastic try everything and comment here if you get any problem.

A Final Note: People it s weekend , it s sunday today.I sat for two hours just to write this blog.People there are many people who know everything, but we need to think of those who don't know and are struggling.Guys I want each of you to be a great programmer and to excel in your campus interviews(Special wishes to my Prist University Students).My motto and aim is to remove the fear of programminf from each and every engineering student , because I personally myself suffered in my first year when my great professor started writing printf and scanf in my first class and
 I was sitting like "Taarein Jammeen per " :P. So guys please help me in developing the students and nation, post queries, refer my blog to friends, let me know if it was useful ,tell me areas to improve ,etc and Wait for the next weekend , depending upon the response, I will continue this C Class to further complex operations.

Cheers!


Ankit