C++ for Embedded · Interview question

What problem do namespaces solve, and how did C handle it?

A strong answer

Namespaces prevent name collisions, two libraries or subsystems each wanting a init(), a Status type, or a MAX constant without clashing at compile or link time. C has no language feature for this, so it relies on a manual convention: prefix every public symbol with a module name (uart_init, spi_init, UART_OK). That works but is verbose, error-prone, and unenforced. C++ namespaces make the grouping a language construct, uart::init and spi::init coexist naturally, accessed via the scope-resolution operator ::. It's the same idea as the C prefix convention, promoted to something the compiler understands and manages, which also enables tooling like targeted using declarations and aliases. And like the rest of the embedded subset, namespaces are compile-time only with zero runtime cost.

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

Namespaces & Code Organization

Group related names to avoid collisions, replace C's prefix conventions, and structure firmware into headers and translation units that compile fast and read clearly.

More Namespaces & Code Organization questions

Browse all 472 interview questions