C Reference ยท ctype.h, math.h and others
C stddef.h: size_t, ptrdiff_t, NULL, offsetof
Reference for stddef.h: the common types size_t, ptrdiff_t and wchar_t, the NULL macro, offsetof, max_align_t, and the C23 additions nullptr_t and unreachable().
<stddef.h> is a small header with the definitions that almost every C program needs and that do not belong anywhere else. It provides the type size_t (the result of sizeof), ptrdiff_t (the result of subtracting two pointers), wchar_t, the null pointer constant NULL, the offsetof macro and max_align_t. C23 adds nullptr_t and unreachable().
Many other headers (<stdio.h>, <stdlib.h>, <string.h>) also define size_t and NULL, so you often get them without including <stddef.h>. Include it when you use them and nothing else gives them to you, and always when you need offsetof or ptrdiff_t.
#include <stddef.h> size_t n = sizeof(int[10]); ptrdiff_t gap = &a[7] - &a[2]; size_t where = offsetof(struct item, price);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <stddef.h> typedef /* unsigned integer */ size_t; typedef /* signed integer */ ptrdiff_t; typedef /* integer */ wchar_t; typedef /* type with the strictest alignment */ max_align_t; /* C11 */ typedef typeof(nullptr) nullptr_t; /* C23 */ #define NULL /* null pointer constant */ #define offsetof(type, member) /* size_t constant expression */ #define unreachable() /* C23 */
| Name | Description |
|---|---|
size_t |
Unsigned type of sizeof results and of object sizes and counts. Printed with %zu. |
ptrdiff_t |
Signed type of the difference of two pointers into the same array. Printed with %td. |
wchar_t |
Integer type that holds a wide character; 32 bits on Linux, 16 bits on Windows. |
NULL |
Expands to a null pointer constant, such as ((void *)0) or, since C23, possibly nullptr. |
offsetof(type, member) |
Offset in bytes of member from the start of type, as a size_t constant. |
max_align_t |
A type as strictly aligned as any scalar type; malloc returns memory aligned for it. |
nullptr_t |
The type of the C23 nullptr constant. Only comparison, assignment to pointers and sizeof make sense. |
unreachable() |
Marks a place that can never be executed, letting the compiler optimize on that assumption. |
Returns: offsetof and the types are compile-time entities. unreachable() never returns.
Errors / undefined behavior: reaching unreachable() while the program runs is undefined behavior. Using offsetof on a bit-field is a constraint violation. Subtracting pointers into different arrays is undefined, and so is a result that does not fit in ptrdiff_t.
Usage notes
size_tis unsigned.a.length - 1wraps around to a huge number when the length is 0; write loops asfor (size_t i = 0; i < n; i++)and compare before subtracting.ptrdiff_tfor signed offsets. It is the right type for the difference of two pointers or for a signed index into a buffer. Both types match the platform: 64 bits on a 64-bit Linux system, so%zuand%tdare the portable specifiers.NULLis a constant, not a type. In C its exact form is implementation-defined (an integer0,((void *)0)and, in C23,nullptrare all permitted), so never rely onsizeof(NULL)and castNULLwhen passing it to a variadic function.offsetofneeds no running object. It works with a type name, so you can use it in static assertions. It accounts for padding, which is why the offsets here are 0, 4 and 8 on the runner (a 64-bit Linux target); other ABIs can differ.- The container-of trick. Subtracting
offsetoffrom a member pointer recovers the enclosing struct, as in the first example. It is how intrusive linked lists in the Linux kernel work. It is only valid if the member really lives in an object of that struct type. unreachable()is a promise, not an assertion. If you are wrong, the compiler may produce code that jumps into arbitrary instructions. During development use assert first; addunreachable()only where you can prove the case is impossible.wchar_tportability. Because it is 16-bit on Windows and 32-bit on Linux, code that stores text inwchar_tis not binary-portable; see wchar.h and uchar.h.- Standard version:
size_t,ptrdiff_t,wchar_t,NULLandoffsetofsince C89;max_align_tsince C11;nullptr_tandunreachable()since C23.