Microsoft Interview Question






Comment hidden because of low score. Click to expand.
3
of 0 vote

This is a topic that has created a lot of confusion in internet forums. Let me put it clear here. Reply me, if you don't understand after reading this.

Declaration and definition have different meanings in different contexts. Generally, a declaration describes to the compiler to bind an identifier with a specific data type. Compiler does NOT allocate storage when it sees a declaration.
A definition tells the compiler to allocate storage for the declared data type.

Function Declaration vs Definition
----------------------------------
Declaration of a function means telling to the compiler about the function's name, arguments and return type. This is done by the prototype of the function.
Eg:

int func(int x, float y) ();

Definition of a function means telling to the compiler about the details of a function and what it should do when the function is called. This is when, compiler allocates storage.

Eg:

int func(int x, float y) {
char response;
int value;
....
....
return value;
}


Variable Declaration vs Definition
----------------------------------
For automatic, static & register storage class variables, declaration and definition are the same. There is no difference between both terms. By that, I mean, the binding of identifier with data type and allocation of storage will happen at the same time.

But, when it comes to an extern variable, their meaning differ. For example, when you write
extern int i; /* extern declaration of i */
it WONT allocate storage. It tells to the compiler that a variable called "i" of integer type has already been defined (binding int data type with variable "i" and allocating space for it) in some other file. So, connect me to that memory. This is basically a linking process and is taken care by the linker.
Thus definition and declaration hold different meaning, when it comes to extern storage class variables.

- Metallicatony September 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That was good.

- charlie September 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well the same thing can be said about the objects. When you declare an object of the class, no memory is allocated. The "new" operator allocated the memory.

- AD September 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C declaration is just declaring a variable at the beginning of the program
eg; int i;
And definition is assigning a value ie, intialisation
eg; int i=5;
Hop this is r8....

- Murali June 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

writing just int i has allocated the required 2 bytes or 4 bytes. So that has defined the variable.

- anonymous October 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Murali,

for variables like int i; declaration and definition (memory) happen at same time. please read the first answer to understand it. Thanks.

- yours.ramesh February 12, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More