C Programming · Interview question

What's the difference between a declaration and a definition?

A strong answer

A declaration introduces a name and its type but allocates nothing, int foo(int); or extern int counter;. It tells the compiler "this exists somewhere, here's how to use it." A definition actually creates the entity: the function body int foo(int x) { ... }, or int counter = 0; which reserves storage. The compiler only needs declarations to generate calling code, it trusts the promise. The linker is what insists a definition exists, which is why a missing definition shows up as a link-time "undefined reference," not a compile error. You can declare a symbol many times but must define it exactly once (the One Definition Rule).

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

The C Compilation Pipeline

What gcc actually does between your .c file and a flashable binary, and why 'undefined reference' and 'implicit declaration' are different bugs at different stages.

More The C Compilation Pipeline questions

Browse all 472 interview questions
What's the difference between a declaration and a definition? | EmbeddedPrep.io