C++ for Embedded · Interview question

Give an example of valid C that won't compile as C++.

A strong answer

The classic is int *p = malloc(n);, in C, void* implicitly converts to any object pointer, but C++ requires an explicit cast: int *p = static_cast<int*>(malloc(n));. Others: using C++ keywords as identifiers (int new = 5; or a variable named class), relying on implicit narrowing that C++ flags, and certain implicit enum-to-int conversions if you've moved to enum class. These differences exist because C++ tightened rules that allowed silent bugs, the implicit void* conversion in particular hid type mismatches. Porting C to C++ is mostly mechanical, but you'll fix casts and the occasional keyword collision.

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
Give an example of valid C that won't compile as C++. | EmbeddedPrep.io