C Standard Library Numeric Arithmetic Functions Explained
A practical tour of numeric arithmetic in the C standard library: math.h functions, rounding, integer helpers, NaN checks and why M_PI is not portable.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
A practical tour of numeric arithmetic in the C standard library: math.h functions, rounding, integer helpers, NaN checks and why M_PI is not portable.
C exposes numeric limits as macros in limits.h, float.h and stdint.h. See integer and floating-point ranges, platform differences and overflow checks.
C has no equivalent of C++ type transformation traits. Learn how implicit conversions, casts, qualifiers and C23 typeof_unqual change types in C.
C has no equivalent of C++ type relationship traits. See how compatibility, conversions and struct composition work, and how to check types yourself.
C has no type_traits header. See the nearest C answer for each C++ trait category and how to build a small reusable traits header.
C has no function objects library, but function pointers and context structs cover the same ground. See nullary callbacks, dispatch tables and atexit.
C has no monadic functions, but Option and Either patterns, a hand-written bind and goto cleanup cover the same ground. See working examples.
C has no std::reference_wrapper because it has no references. Use pointers, const pointers, arrays of pointers and void pointer contexts for the same results.
Standard C has no std::bind equivalent. Use function pointers with a user-data pointer, or a struct holding the function and its bound arguments.
C has no dynamic array type. Build a growable array from malloc, calloc, realloc and free, and learn how to resize it safely without leaks.
Learn which C standard library functions return uninitialized memory, how malloc, calloc, realloc and aligned_alloc differ, and how to use them safely.
C manages heap memory with malloc, calloc, realloc and free. See how each works, how to avoid leaks and double frees, and what C23 adds.
A practical guide to string.h and cstring: copy, concatenate, compare and search C strings, and avoid buffer overflows with snprintf and bounded copies.
The C standard library has no library named Program Execution; these functions in stdlib.h control startup commands and termination. See how exit, atexit and abort differ.
How to measure time in C with time.h: CPU time with clock(), timestamps with time(), nanosecond values with timespec_get, and monotonic clocks for benchmarks.
C has no dedicated time point library. See how time.h types stand in for C++ time points and durations, and how to time code with a monotonic clock.
The C clocks library gives you clock(), clock_t and CLOCKS_PER_SEC for CPU time. Learn what they measure, platform differences and POSIX clock_gettime options.
A guide to time.h in C: read the current time, convert with struct tm, format dates with strftime, do date math with mktime, and avoid thread-safety traps.
C has no equivalent of C++ std::chrono. Use time.h for calendar time, clock for CPU time and a monotonic clock for elapsed time, and avoid the common pitfalls.
C has no equivalent of C++ std::ratio. Build a normalised fraction struct with gcd reduction and overflow checks, or choose floating or fixed-point arithmetic.