0 day streak
Code library

Intermediate C exercises

Read these Intermediate C snippets line by line — each one comes with a written breakdown of what the code does and why.

C · Intermediate

Pointer Arithmetic with Arrays

In C, array names decay into pointers. Adding an integer to a pointer (pointer arithmetic) moves the pointer by that many elements, not bytes.

Arrays & ListsMemory Management
Open snippet →
C · Intermediate

Dynamic Memory for Structs

Using malloc to allocate memory for structures on the heap is essential when the size or lifetime of the data isn't known at compile time.

Memory ManagementData Types
Open snippet →
C · Intermediate

Optimization with the restrict Keyword

The 'restrict' qualifier is a hint to the compiler that for the lifetime of the pointer, only the pointer itself or a value derived from it will be used to access the object it points to. This allo…

PerformanceMemory ManagementSyntax
Open snippet →
C · Intermediate

Variadic Functions using stdarg.h

Variadic functions allow you to pass a variable number of arguments to a function. This is achieved using the macros defined in <stdarg.h>. You must provide at least one fixed argument (like 'count…

FunctionsSyntax
Open snippet →
C · Intermediate

Memory Efficiency with Unions

A union allows different data types to be stored in the same memory location. The size of the union is determined by its largest member. This is particularly useful in systems programming for overl…

Data TypesMemory Management
Open snippet →
C · Intermediate

Safe Multi-line Macros with do-while(0)

Wrapping a multi-statement macro in a 'do { ... } while (0)' block ensures it behaves as a single statement. This prevents logical errors when the macro is used inside 'if' statements without curly…

SyntaxBest PracticesMemory Management
Open snippet →
C · Intermediate

Designated Initializers for Structures

Designated initializers allow you to initialize structure members by name rather than by position. This makes code more robust against changes in the structure definition and improves readability b…

SyntaxBest Practices
Open snippet →
C · Intermediate

The volatile Type Qualifier

The 'volatile' keyword tells the compiler that a variable's value can change at any time without any action being taken by the code nearby. This prevents the compiler from applying optimizations th…

SyntaxPerformance
Open snippet →
C · Intermediate

Compile-time Assertions with _Static_assert

_Static_assert (introduced in C11) allows checking conditions during the compilation phase rather than at runtime. If the condition is false, the compiler generates an error and stops, preventing t…

SyntaxBest Practices
Open snippet →
C · Intermediate

Inlining Functions for Performance

The 'inline' keyword is a hint to the compiler to replace function calls with the actual function body. This eliminates function call overhead (stack manipulation, jumping) for small, frequently us…

FunctionsPerformance
Open snippet →
C · Intermediate

Passing Multidimensional Arrays to Functions

In C, multidimensional arrays are stored in row-major order. To correctly calculate memory offsets, a function receiving a 2D array must know the size of the columns (the second dimension), while t…

Arrays & ListsFunctions
Open snippet →
C · Intermediate

Safe String Formatting with snprintf

The snprintf function is a safer alternative to sprintf because it requires a maximum buffer size. This prevents buffer overflows by ensuring that no more than 'n' characters are written, including…

FunctionsBest Practices
Open snippet →
C · Intermediate

Atomic Operations with stdatomic.h

The stdatomic.h header (C11) provides types and functions for atomic operations, which are essential for thread-safe programming without the overhead of heavy locks. These operations are guaranteed…

Memory ManagementPerformance
Open snippet →
C · Intermediate

Precise String Searching with strcspn

The strcspn function calculates the length of the initial segment of a string that consists entirely of characters NOT in a specified set. It is highly efficient for finding the first occurrence of…

SyntaxPerformance
Open snippet →
C · Intermediate

Customizing Stream Buffering with setvbuf

The setvbuf function allows manual control over how a file stream (like stdout) is buffered. You can choose between fully buffered (_IOFBF), line buffered (_IOLBF), or unbuffered (_IONBF). Disablin…

Memory ManagementPerformance
Open snippet →
C · Intermediate

Robust Numeric Parsing with strtol

Unlike the simpler atoi(), strtol() provides robust error checking and handles different number bases. It detects overflow, empty inputs, and tells you exactly where the numeric portion ends by upd…

SyntaxBest Practices
Open snippet →
C · Intermediate

Variadic Functions with stdarg.h

Variadic functions allow a function to accept an indefinite number of arguments. The 'stdarg.h' header provides macros like 'va_start', 'va_arg', and 'va_end' to traverse the argument list safely b…

FunctionsSyntax
Open snippet →
C · Intermediate

Optimization with the restrict Qualifier

The 'restrict' keyword is a hint to the compiler that the pointer is the only means to access the object it points to within that scope. This eliminates potential 'pointer aliasing', allowing the c…

PerformanceMemory Management
Open snippet →
C · Intermediate

Unions for Type Punning

Unions allow the same memory location to be interpreted as different types. This 'type punning' is often used at an intermediate level to inspect the raw binary representation of data types like fl…

Data TypesMemory Management
Open snippet →
C · Intermediate

Structural Padding and offsetof

Compilers insert 'padding' bytes between structure members to ensure they are aligned with memory boundaries for performance. The 'offsetof' macro from 'stddef.h' allows you to see the exact byte o…

Memory ManagementPerformance
Open snippet →
C · Intermediate

Automatic Cleanup with atexit

The atexit function registers a callback that is automatically executed when the program terminates normally via return from main or exit(). This ensures that essential cleanup logic runs without n…

FunctionsBest Practices
Open snippet →
C · Intermediate

Array Decay and Pointer Arithmetic

In C, arrays 'decay' into pointers to their first element when passed to functions. This means the size information is lost inside the function. Use pointer arithmetic (ptr + offset) to navigate th…

Arrays & ListsMemory Management
Open snippet →
C · Intermediate

Determining File Size with fseek and ftell

To measure a file's size without reading every byte, you can seek to the end of the stream and query the position. This is essential for allocating exactly enough memory to load a file's content in…

FunctionsBest Practices
Open snippet →