C has no pure virtual functions. A pure virtual function (virtual void f() = 0;) is a C++ feature that makes a class abstract, and C has no classes, inheritance or virtual dispatch. You can get the same effect by hand with a struct of function pointers, and you can enforce "must be implemented" with a constructor function that rejects incomplete tables.
This article shows what the C++ feature does, then builds a safe C equivalent using a vtable struct, NULL checks and assert.
In C++ a pure virtual function is declared with = 0. A class with at least one pure virtual function is abstract, so you cannot create objects of it, and every concrete derived class must override the function or it stays abstract too.
The compiler enforces both rules: it refuses to instantiate Shape and refuses to instantiate a derived class that did not implement area(). C offers nothing comparable, so everything below is convention plus run-time or constructor-time checks.
C is a procedural language. It has structs and function pointers, but no member functions, no inheritance and no implicit virtual table. Calling a function through a pointer is the only form of run-time dispatch the language provides, and the compiler does not check that a pointer has been assigned.
Because of this, the C++ guarantee ("a concrete type always has an implementation") must be re-created by the programmer. The usual technique is the one used by large C code bases such as the Linux kernel, where struct file_operations is a table of function pointers that each driver fills in.
Define the interface as a struct of function pointers, taking the object as an explicit first argument. Each concrete type has its own data and a static table of implementations.
The key points are:
Shape is the abstract base. Callers use only shape_area() and shape_name(), never the concrete type.Rectangle places Shape base as its first member, so a pointer to the rectangle can be converted to a pointer to its first member and back. This is guaranteed by the C standard for the first member of a struct.shape_init() plays the role of the compiler check: a table without area or name fails the assert as soon as an object is created, instead of crashing later on a null call.destroy is optional, like a virtual function with a default.assert is disabled when NDEBUG is defined. For production code that must reject bad tables in release builds, replace it with an explicit check that returns an error code.
If a slot may legitimately be missing, test it before calling and report the error. This is the "default behaviour" alternative to a hard failure.
The second call prints its message to standard error instead. Note void (*perform)(void): in C before C23, an empty parameter list () means "unspecified arguments", not "no arguments", so always write (void).
| Aspect | C++ pure virtual function | C function pointer table |
|---|---|---|
| Declaration | virtual void f() = 0; | void (*f)(Shape *self); |
| Enforcement | Compile time | Constructor check or run-time test |
| Abstract type cannot be created | Yes, compiler error | Only by convention |
| Dispatch | Hidden vtable pointer | Explicit ops pointer |
| Object argument | Implicit this | Explicit self |
| Memory safety | Checked by the type system | Casts are unchecked |
If you want real abstract classes, use C++. In C, keep the table static and const, validate it in one place, and keep the abstract interface small.
No. The syntax and the abstract class rules are C++ only. In C you simulate the interface with function pointers and check that required pointers are set.
It is undefined behaviour and usually crashes with a segmentation fault. Validate the table when the object is created, or check the pointer before every call.
Conceptually yes. A C++ compiler generates a hidden vtable and a pointer to it in each object. In C you write the table and the pointer yourself and pass the object explicitly as an argument.
Use C++ if you can, since it checks abstract classes and overriding at compile time. Use the C pattern when the code base must stay in C, such as in kernels or embedded firmware.
Pure virtual functions exist only in C++. In C, build an abstract interface from a const table of function pointers, make the object carry a pointer to its table, and validate required entries when an object is created. This reproduces the structure of C++ abstract classes, but the guarantees come from your own checks rather than the compiler.