Wednesday, November 28, 2012

Difference between declaration,assignment & initialization


There is few things in C sound like the same but all are carrying different meaning.
I found many people who have got good programming knowledge of C (i don't... Haha).
But still they are not clear with very basic terminology and believe me, for clearing the interview this is the imp thing that interviewer looks into you .

Sometime what happens .....we know what we have been asked for ....but we start answering something else because we know that thing by some different name(terminology)...

You should be very much clear with the standard terminology that you are going to use in Interview.....

Once one of my friend has gone to interview as fresher in Quinnox Bangalore. He answered some questions well but the moment interviewer asked

Can you please explain the difference between Initialization,Assignment and Declaration???


He was like bla bla bla .....

So it happens in many cases, specially if you are fresher Be ready for such kind of question.....

Anyway we are discussing here:

Initialization , assignment , declaration

******************************************************
declaration means creating a variable, each variable that are used in program must declare.

Let's assume we are creating one variable.


                int a;


so this comes under variable declaration......
we can say we are creating a new variable....
creating variable is local word don't use in interview.
we are just declaring it so that we can use it in further part of programming...
once we declare the variable we can't declare the same variable again otherwise compiler will get ambiguity..

later if we need to put some value inside this variable....
So we go like this...
 
 

              a=10;


This is purely assignment....and assignment can be done n no. of times in a program.there is no such restriction on it.

Assignment: throwing away the old value of a variable and replacing it with a new one.It can be done anywhere in the whole program....


"=" is known as assignment operator(right to left).

In this example earlier "a" was holding garbage value.now we replaced it with 10.

 

Initialization: Declaring a variable and assigning a value at the declaration time only is called initializing a variable


eg:

       

        int a=10;      //initializing variable....

      

Initialization = declaration + assignment    at single step.....


Hope that Diagram makes you more clear about these 3 term.


In later post we'll discuss about
Function calling
Function Definition
and Function declaration


and of course how many ways to read ( int *p;) in front of interviewer.

No comments:

Post a Comment