Programming Fundamentals · Interview question

Why does scanf need an & in front of its arguments but printf doesn't?

A strong answer

printf only reads the values you pass it, they go into the function by value. scanf has to write the parsed input back into your variables, which means it needs to know where each variable lives in memory. That's what & gives you: the address of the variable. Inside scanf, dereferencing that address writes the new value to the right slot. If you pass scanf the value of an int instead of its address, it'll try to write through that integer as if it were a pointer, almost always a crash. The exception is arrays and strings, because in C an array's name already decays to a pointer to its first element, so scanf("%s", name) is correct without &.

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

Input & Output

How a program talks to the outside world. printf and scanf in C: format specifiers, common traps, and why the return value matters.

More Input & Output questions

Browse all 472 interview questions