On this page
<stdlib.h> & <string.h>
Prev: stdio.h & File I/O | Up: stdlib | Next: stdint, stdbool & limits
Rel: Dynamic Memory & Allocators, Function Pointers & Callbacks
Ref: cppreference — Null-Terminated Byte Strings
Tags: #public #prog #c #stdlib
Contents
- > `<string.h>`: `memcpy`, `memmove`, `memset`, `memcmp`
- > `<stdlib.h>`: Safe Integer Parsing (`strtol`) & Sorting
<string.h>: memcpy, memmove, memset, memcmp
memcpy(dst, src, n): fast raw copy, undefined behavior if regions overlap.memmove(dst, src, n): safe whendstandsrcoverlap (e.g., shifting elements inside one array).memset(dst, byte, n): fillsnbytes with a single byte value (commonly0).memcmp(a, b, n): lexicographical byte comparison (0if equal). Note: do not usememcmpon structs that may contain uninitialized padding bytes.
<stdlib.h>: Safe Integer Parsing (strtol) & Sorting
Prefer strtol / strtoll over atoi (which silently returns 0 on invalid input and has UB on overflow):
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
bool parse_long(const char *s, long *out) {
char *end = NULL;
errno = 0;
long val = strtol(s, &end, 10);
if (errno != 0 || end == s || *end != '\0') return false;
*out = val;
return true;
}
Linked from
- tuts/c/fundamentals/arrays_and_strings
- tuts/c/pointers_and_memory/function_pointers_and_callbacks
- tuts/c/stdlib
- tuts/c/stdlib/stdint_stdbool_and_limits
- tuts/c/stdlib/stdio_and_file_io
- tuts/c/stdlib/stdlib
- tuts/c/stdlib/stdlib/abort
- tuts/c/stdlib/stdlib/abs
- tuts/c/stdlib/stdlib/aligned_alloc
- tuts/c/stdlib/stdlib/atexit
- tuts/c/stdlib/stdlib/atoi
- tuts/c/stdlib/stdlib/bsearch
- tuts/c/stdlib/stdlib/calloc
- tuts/c/stdlib/stdlib/exit
- tuts/c/stdlib/stdlib/free
- tuts/c/stdlib/stdlib/getenv
- tuts/c/stdlib/stdlib/malloc
- tuts/c/stdlib/stdlib/qsort
- tuts/c/stdlib/stdlib/rand
- tuts/c/stdlib/stdlib/realloc
- tuts/c/stdlib/stdlib/strtod
- tuts/c/stdlib/stdlib/strtol
- tuts/c/stdlib/stdlib/strtoul
- tuts/c/stdlib/stdlib_and_string_h
- tuts/c/stdlib/string
- tuts/c/stdlib/string/memchr
- tuts/c/stdlib/string/memcmp
- tuts/c/stdlib/string/memcpy
- tuts/c/stdlib/string/memmove
- tuts/c/stdlib/string/memset
- tuts/c/stdlib/string/strcat
- tuts/c/stdlib/string/strchr
- tuts/c/stdlib/string/strcmp
- tuts/c/stdlib/string/strcpy
- tuts/c/stdlib/string/strdup
- tuts/c/stdlib/string/strerror
- tuts/c/stdlib/string/strlen
- tuts/c/stdlib/string/strncmp
- tuts/c/stdlib/string/strncpy
- tuts/c/stdlib/string/strstr
- tuts/c/stdlib/string/strtok
- tuts/c/tutorials/hash_table_from_scratch
Related notes
- tuts/c/stdlib Linked here; links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdint_stdbool_and_limits Linked here; links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio_and_file_io Linked here; links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/pointers_and_memory/function_pointers_and_callbacks Linked here; links to this page; shared tags: #c, #prog
- tuts/c/stdlib/stdlib links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib/abort links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib/abs links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib/aligned_alloc links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib/atexit links to this page; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib/atoi links to this page; shared tags: #c, #prog, #stdlib