C Standard Library Stddef H
π
2026-06-14 | π C
# C Standard Library - ``
The `` header file in the C Standard Library defines several standard types and macros. These definitions are fundamental to C programming, particularly in areas involving memory management, object sizing, pointer arithmetic, and structure alignment.
---
## Introduction
`` is a lightweight but essential header that provides standardized, platform-independent type definitions and macros. By using the types defined in this header, developers can write highly portable C code that behaves consistently across different hardware architectures (e.g., 16-bit, 32-bit, and 64-bit systems).
---
## Library Types
The `` header defines the following standard types:
| No. | Type & Description |
| :--- | :--- |
| 1 | **`ptrdiff_t`**
A signed integer type that represents the difference between two pointers. Its exact size is implementation-defined (usually `long` or `long long` on modern 64-bit systems) to ensure it can safely hold the distance between any two pointers in the same array. |
| 2 | **`size_t`**
An unsigned integer type that represents the size of any object in bytes. It is the type returned by the `sizeof` operator and is widely used for array indexing, loop counters, and memory allocation functions (like `malloc`). |
| 3 | **`wchar_t`**
An integer type capable of representing any character in the largest supported extended character set. It is used for wide-character constants and strings (prefixed with `L`). |
### Code Examples for Types
```c
#include
#include
int main() {
// 1. size_t Example
int numbers[] = {10, 20, 30, 40, 50};
size_t array_size = sizeof(numbers) / sizeof(numbers);
printf("Array size: %zu elements\n", array_size);
// 2. ptrdiff_t Example
int *ptr1 = &numbers; // Points to 20
int *ptr2 = &numbers; // Points to 50
ptrdiff_t diff = ptr2 - ptr1;
printf("Pointer difference: %td elements\n", diff); // Output: 3
// 3. wchar_t Example
wchar_t wide_char = L'A';
printf("Wide character: %lc\n", (wint_t)wide_char);
return 0;
}
```
---
## Library Macros
The `` header defines the following macros:
| No. | Macro & Description |
| :--- | :--- |
| 1 | **`NULL`**
A macro that expands to a null pointer constant. It is used to indicate that a pointer does not point to any valid memory location. |
| 2 | **`offsetof(type, member-designator)`**
Expands to an integer constant expression of type `size_t`. It represents the offset in bytes of a structure member from the beginning of the structure. |
### Code Examples for Macros
```c
#include
#include
// Define a sample structure to demonstrate offsetof
struct User {
char id; // 1 byte
int age; // 4 bytes (usually aligned)
double score; // 8 bytes
};
int main() {
// 1. NULL Example
int *ptr = NULL;
if (ptr == NULL) {
printf("The pointer is safely initialized to NULL.\n");
}
// 2. offsetof Example
size_t offset_id = offsetof(struct User, id);
size_t offset_age = offsetof(struct User, age);
size_t offset_score = offsetof(struct User, score);
printf("Offset of 'id': %zu bytes\n", offset_id);
printf("Offset of 'age': %zu bytes\n", offset_age);
printf("Offset of 'score': %zu bytes\n", offset_score);
return 0;
}
```
---
## Technical Considerations & Best Practices
### 1. Format Specifiers for `printf`
When printing variables of types defined in ``, always use the correct C99 format specifiers to prevent undefined behavior and compiler warnings:
* Use `%zu` for `size_t`.
* Use `%td` for `ptrdiff_t`.
### 2. Pointer Arithmetic and `ptrdiff_t`
Subtracting two pointers is only defined by the C standard if both pointers point to elements of the same array (or one element past the end of the array). If they point to unrelated memory locations, the behavior of the subtraction is undefined.
### 3. Structure Padding and `offsetof`
The `offsetof` macro is highly critical because compilers often insert "padding bytes" between structure members to align them with hardware memory boundaries. Never assume the offset of a member based on the sum of the sizes of preceding members; always use `offsetof` for safe serialization, deserialization, or low-level hardware mapping.