On this page
  1. <stdlib.h> & <string.h>
  2. Contents
  3. <string.h>: memcpy, memmove, memset, memcmp
  4. <stdlib.h>: Safe Integer Parsing (strtol) & Sorting

<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

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

Related notes