<stddef.h> defines everyday building blocks of C: size_t for sizes, NULL for null pointers, ptrdiff_t for pointer differences, offsetof for struct layouts, and wchar_t for wide characters.
Remember
#include <stddef.h>
size_t n = sizeof(x); // unsigned size
ptrdiff_t d = p - q; // signed pointer diff
if (ptr == NULL) { ... } // null check
offsetof(Struct, member) // byte offset
print size_t with %zu
print ptrdiff_t with %td
Use
Prefer
Sizes / counts / sizeof
size_t
Pointer subtraction
ptrdiff_t
Null pointer constant
NULL
Approach
How to Use stddef.h in C
Include the header, pick the right type for sizes or diffs, and check pointers before use.
Add #include <stddef.h> (or rely on it via stdio.h / stdlib.h).
Store sizeof results and array lengths in size_t.
Store p - q in ptrdiff_t when both pointers refer to the same array.
Use NULL for empty pointers and offsetof when you need a member’s byte offset.
Piece
Required?
Role
size_t
Common
Unsigned type for sizes and counts
NULL
Common
Null pointer constant
offsetof / ptrdiff_t
As needed
Struct layout / pointer arithmetic
Pseudocode
Pseudocode
include stddef.h
n = sizeof(Thing) // size_t
off = offsetof(Thing, field) // size_t
if ptr == NULL:
handle missing data
else:
use *ptr
d = end - start // ptrdiff_t (same array)
Reference
Cheat Sheet
Keep these facts handy while writing size- and pointer-safe code.
Guard first.if (name == NULL) (or if (!name)) catches a missing pointer.
Safe path.greet("Ada") prints a normal greeting.
Null path.greet(NULL) prints a message instead of crashing on %s.
Pointers only. Do not assign NULL to an int or other non-pointer type.
Example 3 — ptrdiff_t from Pointer Subtraction
Subtracting two pointers into the same array yields an element count as ptrdiff_t.
C
#include <stdio.h>
#include <stddef.h>
int main(void) {
int data[] = { 10, 20, 30, 40, 50 };
int *start = &data[0];
int *end = &data[4];
ptrdiff_t count = end - start;
ptrdiff_t bytes = (char *)end - (char *)start;
printf("Elements between start and end: %td\n", count);
printf("Byte distance (char*): %td\n", bytes);
return 0;
}
Output
Elements between start and end: 4
Byte distance (char*): 16
How It Works
Pick two pointers.start is index 0; end is index 4 in the same array.
Subtract as int*.end - start counts elements → 4 (ptrdiff_t).
Subtract as char*. Casting to char * gives byte distance → 16 when int is 4 bytes.
Print with %td. That is the usual format for ptrdiff_t.
Edge Cases & Pitfalls
Check these before mixing sizes, pointers, and offsets.
format
Print size_t with %zu
Using %d or %u for size_t is non-portable. Prefer %zu (and %td for ptrdiff_t).
signed
Unsigned loop traps
Comparing a negative int to a size_t can surprise you. Keep indexes as size_t when looping to a size_t length.
pointers
Only subtract related pointers
p - q is defined when both point into the same array (or one past the end)—not arbitrary addresses.
offsetof
No bit-fields with offsetof
Applying offsetof to a bit-field member is undefined behavior.
Analysis
Performance Notes
Approach
Cost
Best for
sizeof / offsetof
Compile-time constants
Layouts, buffers, serialization
size_t indexes
Native unsigned arithmetic
Array lengths and loops
Pointer subtraction
Cheap when valid
Spans inside one array
These facilities are tiny at runtime. Correct types and null checks matter more than micro-optimizing size_t itself.
Remember
Key Takeaways
Role: core types for sizes, nulls, diffs, and offsets.
size_t: unsigned sizes; print with %zu.
NULL: check before you dereference.
offsetof / ptrdiff_t: layout and pointer spans.
One line:stddef.h is the small toolkit for “how big,” “where,” and “is this pointer real?”
Frequently Asked Questions
stddef.h is a core C standard header that defines fundamental types and macros used across the library: size_t for sizes, ptrdiff_t for pointer differences, wchar_t for wide characters, NULL for null pointers, and offsetof for structure member offsets. Many other headers include it indirectly.
size_t is unsigned and holds the result of sizeof and sizes passed to malloc. ptrdiff_t is signed and holds the difference between two pointers to the same array (p - q). Use size_t for counts and sizes; use ptrdiff_t when subtracting pointers.
NULL is a macro for a null pointer constant. It represents a pointer that does not point to any object. Compare with if (ptr == NULL) or if (!ptr) before dereferencing. Do not use NULL for non-pointer types.
offsetof(type, member) expands to the byte offset of member within a structure or union of type. It is useful for serialization, binary layouts, and low-level code. Do not apply offsetof to bit-fields; that is undefined behavior.
Often no—stdio.h, stdlib.h, string.h, and others include stddef.h or define size_t. For portable, minimal code that only needs size_t or NULL, #include <stddef.h> explicitly is clearest.
No. char is for narrow characters (typically 1 byte). wchar_t is an integer type for wide characters (often 2 or 4 bytes). Wide string literals use L"text". See wchar.h for wide-character functions; stddef.h only declares the type.