C Programming • C17 baseline, standards-specific features labeled
Designing Stable and Testable C APIs
Design interfaces with explicit ownership, error behavior, and room for implementation changes.
A good C API states whether pointers may be null, who owns returned memory, how long borrowed data remains valid, and how errors are reported. Opaque structs can hide representation and preserve ABI flexibility. Keep initialization and cleanup paired and make operations easy to test.
Example
/* public header */
struct session;
struct session *session_create(void);
int session_run(struct session *value);
void session_destroy(struct session *value);
What to expect
Callers can use the session without depending on its internal fields and release it through the matching destroy function.
Key idea: Document ownership and lifetime in the API; consider status enums when callers need more than success or failure.