Inheritance vs Polymorphism in C: Differences and Examples
C has neither feature built in. See how struct embedding emulates inheritance and function pointer tables emulate polymorphism, with runnable code.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
C has neither feature built in. See how struct embedding emulates inheritance and function pointer tables emulate polymorphism, with runnable code.
C has no classes, yet embedding a base struct in a derived struct emulates inheritance. See init chaining, upcasts, container_of and the limits of the technique.
C has no classes: an object is just storage, and a struct only holds data. See how to build object-style code with functions and opaque types.
C had no standard bit-counting functions until C23 added stdbit.h. Learn portable loops, compiler built-ins and the new stdc_count_ones family.
C has no standard bit rotation function. Use the shift-and-OR pattern with a masked count to rotate bits safely and let the compiler emit a single instruction.
C has no shift library: << and >> are built-in operators. See how they work, how signed values and large counts cause undefined behavior, and how to use them safely.
Bitwise conversion in C uses built-in operators, unsigned fixed-width types and memcpy. See masking, byte packing, sign extension, binary printing and byte swapping.
C lacks a bit_cast function; memcpy between same-sized objects is the portable way to reinterpret bits. Compare it with unions and unsafe pointer casts.
C has no separate arithmetic library: operators are part of the language and math.h, stdlib.h and stdckdint.h add the rest. Learn division, remainder and overflow rules.
Bitwise operations in C are built into the language. See practical recipes for flags, packing fields, parity checks and subsets, plus the shift rules that avoid undefined behavior.
C has no shift library, only the << and >> bitwise operators. Learn their rules, the undefined behaviour to avoid, and common uses.
C has no separate bitwise library; the operators are part of the language. See how to set, clear, test and extract bits and avoid the usual pitfalls.
C does boolean logic with operators, not library calls. See && || !, short-circuiting, bool in stdbool.h and C23, and the bitwise pitfalls.
C has no relational library. Comparisons use built-in operators returning 1 or 0, plus strcmp and qsort callbacks for strings and sorting.
C spreads arithmetic over math.h, stdlib.h, limits.h, float.h, stdint.h, complex.h and tgmath.h. This guide shows what each provides and the common traps.
C has no runtime type identification library like C++ RTTI. See how sizeof, _Generic, typeof and your own type tags let you work with types safely.
C has no pointer cast library, just the cast operator. Learn which pointer conversions are safe, how strict aliasing applies and why memcpy is the right type-punning tool.
C has no equivalent of C++ type_traits. See which operators and headers expose type size, alignment, range and signedness at compile time.
C has no dynamic_cast or RTTI. Learn how tagged unions, header structs and _Generic give you checked type handling in plain C.
Standard C provides gamma and error functions in math.h. Bessel functions are POSIX extensions, not ISO C. Examples, output and portability notes included.