Skip to content

Declaring a function with C linkage

Question

What declaration makes a C++ function callable with C linkage?

Short answer

Prefix it with extern "C", e.g. extern "C" double f(double);, or wrap several declarations in extern "C" { ... }. This switches off C++ name mangling so C code or other languages can link to it; such functions cannot be overloaded. Headers shared with C usually guard the block with #ifdef __cplusplus.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.35, p.233; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question