C Tuple Library: Does C Have Tuples? Use Structs Instead
C does not provide a tuple library like C++ std::tuple. Learn how to group mixed values with structs, arrays and tagged unions, and what to avoid.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
C does not provide a tuple library like C++ std::tuple. Learn how to group mixed values with structs, arrays and tagged unions, and what to avoid.
C formats text with the printf family. See the common specifiers, why snprintf beats sprintf, and how to wrap vsnprintf safely.
C reports system errors through errno, perror and strerror. Learn how to read error codes correctly and design reliable error handling.
C reports errors through return values and errno. Learn how perror, strerror, assert and a cleanup pattern combine into reliable error handling.
The C standard library only provides rand() and srand(). Learn how to seed them, get unbiased numbers in a range and when to use a secure source instead.
C has no std::variant. A tagged union, an enum tag plus a union, gives you the same idea with explicit type tracking and cleanup.
C has no Any type like C++ std::any. Learn how to store mixed types with void pointers, tagged unions and _Generic, with working code.
C has no std::optional equivalent, not even in C23. Use NULL pointers, sentinel values, a bool with an output parameter, or a struct with a has_value field.
Learn C memory allocation with malloc, calloc, realloc, free and aligned_alloc, how to avoid leaks and overflows, and how to write an arena allocator.
C has no smart pointers. See portable patterns that give similar safety: goto cleanup, atomic reference counting, cleanup attribute and arenas.
C has no tuples library, so you use structs. See how to return multiple values, write generic pair types with macros and compare tuple-like structs.
A practical guide to complex.h in C: complex types, the constant I, arithmetic, magnitude and phase functions, CMPLX in C11, and common pitfalls.
C library utilities are the general-purpose functions in stdlib.h and stddef.h, from exit and atexit to qsort and offsetof. See how each one works.
C has no equivalent of C++ type_traits. See how _Generic, typeof and static_assert approximate compile-time type checks and where they fall short.
A practical guide to time.h in C: getting the current time, formatting dates, thread-safe conversions and measuring elapsed time the right way.
C has no future or promise type. You build one from threads, a mutex and a condition variable, or use event loops for asynchronous I/O.
C has had a standard thread library since C11. Learn what threads.h offers, how to detect support, and when to use pthreads or Windows threads instead.
Since C11, stdatomic.h provides atomic types and operations, so threads can share counters and flags safely without a mutex. Examples with memory orders included.
The C standard library has no regex support. Use POSIX regex.h on Unix-like systems or PCRE2 for portable, Perl-style matching, with working code.
Standard C has no filesystem library like C++17. Learn what stdio provides and how POSIX or Windows APIs cover directories and metadata.