Table of Contents

<stddef.h>

<stddef.h> provides fundamental types and macros: size_t (for sizes), ptrdiff_t (pointer differences), wchar_t, NULL, and offsetof(). Most headers include it transitively, but it's essential to include directly for portable code.

Use size_t for array indices and loop counters, never int.

Example

This example queries structure member offsets and demonstrates size_t usage.

// compile: gcc -o stddefexample stddefexample.c
// run: ./stddefexample
// description: query struct member offsets and sizes
 
#include <stddef.h>
#include <stdio.h>
 
struct packet {
    char type;
    int length;
    unsigned int checksum;
};
 
int main() {
    printf("sizeof(packet) = %zu\n", sizeof(struct packet));
    printf("offset(type) = %zu\n", offsetof(struct packet, type));
    printf("offset(length) = %zu\n", offsetof(struct packet, length));
    printf("offset(checksum) = %zu\n", offsetof(struct packet, checksum));
 
    return 0;
}