C · Expert
Strict aliasing allows the compiler to assume that pointers of different types do not alias the same memory. The 'restrict' qualifier (C99) explicitly informs the compiler that for the lifetime of…
PerformanceBest Practices
Open snippet →C · Expert
A flexible array member is an unsized array declaration at the end of a struct. It allows for a single contiguous memory allocation for both the metadata (header) and the payload (data). This avoid…
Arrays & ListsMemory Management
Open snippet →C · Expert
The 'volatile' qualifier prevents the compiler from caching a variable's value in a register, forcing it to read from memory every time. 'sig_atomic_t' is a type that can be accessed as an atomic e…
SyntaxMemory Management
Open snippet →C · Expert
Opaque pointers (often called 'handles' or the PIMPL idiom) allow for complete data encapsulation in C. By only providing a forward declaration in the header and defining the structure in the sourc…
FunctionsBest Practices
Open snippet →C · Expert
The X-Macro pattern is a powerful preprocessor technique used to maintain multiple related lists (like enums, strings, or switch cases) in sync. You define a list once and then 'apply' it by redefi…
SyntaxBest Practices
Open snippet →C · Expert
Uses C11's stdatomic.h to perform a lock-free update. atomic_compare_exchange_weak checks if the current value matches 'expected'; if so, it updates it to 'desired'. If not, it updates 'expected' w…
Memory ManagementPerformance
Open snippet →C · Expert
Compound literals allow creating unnamed objects on the fly. They are lvalues, meaning you can even take their address. This is extremely useful for passing complex arguments to functions without c…
SyntaxMemory Management
Open snippet →C · Expert
The __VA_ARGS__ identifier is used in the macro expansion to represent the variable arguments passed to the macro. This allows creating powerful wrappers around functions like printf while automati…
SyntaxFunctions
Open snippet →C · Expert
Introduced in C11, _Static_assert allows checking conditions at compile time rather than runtime. If the expression is false, the compiler generates an error with the provided string, ensuring that…
SyntaxBest Practices
Open snippet →C · Expert
Designated initializers allow you to initialize specific array indices or struct members by name/index. For arrays, this is particularly useful for mapping non-sequential constants (like error code…
SyntaxArrays & Lists
Open snippet →C · Expert
Introduced in C11, the _Thread_local storage class specifier defines variables that have thread storage duration. Each thread receives its own separate instance of the variable, initialized once pe…
Memory ManagementPerformance
Open snippet →C · Expert
Using a pointer to an array with a variable size (VLA) as a function parameter allows for natural multidimensional indexing (matrix[i][j]) while maintaining type safety. Unlike a pointer-to-pointer…
Arrays & ListsFunctions
Open snippet →C · Expert
The _Noreturn keyword (C11) indicates that a function will not return to its caller, typically because it terminates the program or performs a long jump. This provides a hint to the compiler for op…
FunctionsControl Flow
Open snippet →C · Expert
In C, arithmetic operations on types smaller than 'int' (like char or short) trigger 'integer promotion', converting the operand to an 'int' first. Performing a bitwise NOT (~) on an unsigned char…
Data TypesSyntax
Open snippet →C · Expert
The result of subtracting two pointers is of the signed integer type 'ptrdiff_t'. This operation is only defined if both pointers point to elements within the same array or one past the last elemen…
Memory ManagementData Types
Open snippet →C · Expert
Introduced in C11, _Generic provides a way to write type-generic macros. It acts like a compile-time switch statement that selects an expression based on the type of a controlling expression, allow…
SyntaxFunctions
Open snippet →C · Expert
setjmp and longjmp allow for non-local jumps that bypass the standard function call/return mechanism. This is often used to implement exception-like error recovery where a deeply nested function ca…
Control FlowFunctions
Open snippet →C · Expert
Opaque pointers (or 'PIMPL' in C) hide the implementation details of a struct from the user. By only providing a forward declaration in the header, the compiler prevents users from accessing intern…
OOPBest Practices
Open snippet →C · Expert
X-Macros are a preprocessor technique for generating repetitive code from a single master list. This ensures that enums, string arrays, and other related structures remain synchronized, reducing ma…
SyntaxBest Practices
Open snippet →C · Expert
Flexible Array Members (introduced in C99) allow a structure to end with an unsized array. This technique enables a single heap allocation to contain both the metadata header and the variable-lengt…
Memory ManagementArrays & ListsData Types
Open snippet →C · Expert
Bit-fields allow the definition of structure members with explicit bit widths. This is essential for hardware-level programming or compacting data for network protocols. However, bit-field layout i…
Memory ManagementPerformanceData Types
Open snippet →C · Expert
Function pointers enable polymorphism in C. By using 'void*' parameters in a callback signature, you can create generic algorithms (like qsort) that operate on any data type. The callback abstracts…
FunctionsBest Practices
Open snippet →C · Expert
'sig_atomic_t' is an integer type that can be accessed as an atomic entity even in the presence of asynchronous interrupts (signals). Combined with 'volatile', it prevents the compiler from caching…
PerformanceMemory ManagementData Types
Open snippet →C · Expert
Non-local jumps allow control flow to bypass normal function call and return sequences across the call stack. Calling `setjmp` saves the current execution state (registers and stack context) into a…
Control FlowFunctionsBest Practices
Open snippet →C · Expert
Duff's Device is a famous technique relying on legal C syntax where a `switch` statement interleaves directly into a `do-while` loop. It achieves manual loop unrolling with minimal branch condition…
Control FlowPerformanceSyntax
Open snippet →C · Expert
Intrusive linked lists embed node pointers directly inside payload structures rather than wrapping payload pointers inside list nodes. The `container_of` macro uses `offsetof` from `<stddef.h>` to…
Memory ManagementData TypesArrays & Lists
Open snippet →C · Expert
An Arena Allocator manages linear bump allocation over a fixed byte buffer to avoid malloc overhead and fragmentation. High-performance systems require data memory addresses aligned to hardware bou…
Memory ManagementPerformanceBest Practices
Open snippet →C · Expert
C11 introduced the _Generic keyword, enabling type-based compile-time macro dispatch without runtime overhead or name mangling. The compiler evaluates the static type of the controlling expression…
Data TypesFunctionsBest Practices
Open snippet →C · Expert
Object-oriented dynamic dispatch can be cleanly implemented in standard C by nesting a base structure containing a pointer to a virtual method table (VTable). Sub-structures place the base struct a…
OOPFunctionsBest Practices
Open snippet →C · Expert
Branch mispredictions in performance-critical loops trigger severe CPU pipeline flushes. By leveraging arithmetic bit-shifts to construct sign masks, conditional selections like maximum or absolute…
PerformanceBest Practices
Open snippet →