C++ for Embedded · Interview question

What is name mangling and why does it matter when mixing C and C++?

A strong answer

C++ encodes a function's parameter types into its linker symbol name, so two overloads init(int) and init(float) get distinct mangled names like _Z4initi and _Z4initf. C does no mangling; init's symbol is just init. The consequence: if a C++ translation unit calls a function defined in C (or vice versa), the names won't match at link time and you get "undefined reference." The fix is extern "C", which tells the C++ compiler to use C linkage (no mangling) for those declarations, you wrap C headers and any function the C world references by name. This is critical for interrupt handlers: the vector table is C and references handlers by their plain names, so an ISR written in C++ must be extern "C" or the linker won't connect it.

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

C++ vs C for Embedded

What C++ adds over C on a microcontroller: classes, RAII, templates, references, and the zero-overhead principle that makes those abstractions free.

More C++ vs C for Embedded questions

Browse all 472 interview questions
What is name mangling and why does it matter when mixing C and C++? | EmbeddedPrep.io