C stddef.h Header

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is stddef.h?

<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
UsePrefer
Sizes / counts / sizeofsize_t
Pointer subtractionptrdiff_t
Null pointer constantNULL

How to Use stddef.h in C

Include the header, pick the right type for sizes or diffs, and check pointers before use.

  1. Add #include <stddef.h> (or rely on it via stdio.h / stdlib.h).
  2. Store sizeof results and array lengths in size_t.
  3. Store p - q in ptrdiff_t when both pointers refer to the same array.
  4. Use NULL for empty pointers and offsetof when you need a member’s byte offset.
PieceRequired?Role
size_tCommonUnsigned type for sizes and counts
NULLCommonNull pointer constant
offsetof / ptrdiff_tAs neededStruct 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)

Cheat Sheet

Keep these facts handy while writing size- and pointer-safe code.

TopicDetail
Header#include <stddef.h>
size_tUnsigned; result of sizeof; print with %zu
ptrdiff_tSigned; result of p - q; print with %td
NULLNull pointer constant
offsetof(T, m)Byte offset of member m in type T
wchar_tWide character type (see wchar.h)
Array lengthsizeof(a) / sizeof(a[0])

Live Preview

Simulate common stddef.h ideas: sizes, null checks, and pointer differences (illustrative layout numbers).

Browser simulation of stddef.h concepts (same idea as the C examples below).

Result
sizeof(Employee) → size_t (e.g. 64)
offsetof(Employee, salary) → size_t (e.g. 56)

/* padding may sit between members */

Worked Walkthrough — Size and Offset

Trace measuring a struct with sizeof and finding where salary sits with offsetof.

Calculation
struct Employee {
  int id;          // often 4 bytes at offset 0
  char name[50];   // 50 bytes
  // padding?      // compiler may insert bytes
  double salary;   // often 8-byte aligned
};

total = sizeof(Employee)           // size_t, e.g. 64
off   = offsetof(Employee, salary) // size_t, e.g. 56

Not simply 4 + 50 — padding for alignment matters ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Struct sizes and offsets can vary by platform; sample output is typical for 64-bit GCC.

Example 1 — sizeof with size_t and offsetof

Measure a structure and find the byte offset of a member.

C
#include <stdio.h>
#include <stddef.h>

typedef struct {
    int id;
    char name[50];
    double salary;
} Employee;

int main(void) {
    size_t total = sizeof(Employee);
    size_t off = offsetof(Employee, salary);

    printf("Size of Employee: %zu bytes\n", total);
    printf("Offset of salary: %zu bytes\n", off);
    printf("sizeof(int)=%zu, sizeof(double)=%zu\n",
           sizeof(int), sizeof(double));

    return 0;
}

How It Works

  1. Store sizeof in size_t. sizeof(Employee) returns the whole struct size, including padding.
  2. Ask for a member offset. offsetof(Employee, salary) is the byte index of salary from the start.
  3. Expect padding. Alignment for double means the offset is not simply 4 + 50.
  4. Print with %zu. That is the correct format for size_t—not %d.

Example 2 — NULL Pointer Check

Always test pointers before dereferencing. NULL means “points nowhere.”

C
#include <stdio.h>
#include <stddef.h>

void greet(const char *name) {
    if (name == NULL) {
        printf("No name provided.\n");
        return;
    }
    printf("Hello, %s!\n", name);
}

int main(void) {
    greet("Ada");
    greet(NULL);
    return 0;
}

How It Works

  1. Guard first. if (name == NULL) (or if (!name)) catches a missing pointer.
  2. Safe path. greet("Ada") prints a normal greeting.
  3. Null path. greet(NULL) prints a message instead of crashing on %s.
  4. 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;
}

How It Works

  1. Pick two pointers. start is index 0; end is index 4 in the same array.
  2. Subtract as int*. end - start counts elements → 4 (ptrdiff_t).
  3. Subtract as char*. Casting to char * gives byte distance → 16 when int is 4 bytes.
  4. 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.

Performance Notes

ApproachCostBest for
sizeof / offsetofCompile-time constantsLayouts, buffers, serialization
size_t indexesNative unsigned arithmeticArray lengths and loops
Pointer subtractionCheap when validSpans inside one array

These facilities are tiny at runtime. Correct types and null checks matter more than micro-optimizing size_t itself.

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.

Next: stdint.h

Use portable fixed-width integers like int32_t and uint64_t.

stdint.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful