scanf returns the count of successfully parsed items, or EOF on input failure / end of stream. If your format string says "%d %d" and the user types "5 hello", you get 1 back, only the first %d matched. If you ignore the return value, the second variable is uninitialized garbage and your program proceeds as if everything worked. The defensive habit is if (scanf("%d", &n) != 1) { error… }, it catches malformed input before bad data spreads through the rest of the program. This is the same lesson as checking every system-call return value: ignoring failure modes is how silent corruption begins.
Programming Fundamentals · Interview question
What does scanf return, and why should you check it?
A strong answer
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
Why does scanf need an & in front of its arguments but printf doesn't?What's the difference between stdout and stderr, and when do you use each?You read a number with %d and then read a character with %c, but the %c doesn't wait for input. Why?Why is scanf("%s", name) dangerous, and what should you do instead?
Browse all 472 interview questions