C Programming · Interview question

Why does changing one header file trigger a rebuild of many source files?

A strong answer

Because #include is a preprocessor operation that literally pastes the header's text into every .c that includes it, before compilation. So a header isn't "linked", its contents become part of each translation unit. When the header changes, every translation unit that included it now has different source text and must be recompiled. Build systems track this dependency and rebuild all dependents. This is also why minimizing what you put in headers (forward declarations instead of full includes, keeping implementation in .c files) speeds up incremental builds in large C codebases.

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
Why does changing one header file trigger a rebuild of many source files? | EmbeddedPrep.io