Programming Fundamentals · Interview question

What's the difference between declaring, initializing, and assigning a variable?

A strong answer

Declaring (int x;) asks the compiler to reserve memory of a specific type and give it a name. The value of that memory is undefined, whatever bits were already there. Initializing (int x = 5;) declares and gives it a starting value in one step. Assigning (x = 7;) writes a new value into an already-declared variable. You can declare-and-initialize once per variable, but you can assign as many times as you want. Always prefer declare-and-initialize on the same line, because reading from an uninitialized variable is undefined behavior.

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

Variables & Data Types

Named boxes that hold a value. Pick a type, declare, assign, and learn why C makes you say what kind of thing each box holds.

More Variables & Data Types questions

Browse all 472 interview questions