Skip to content

<string.h>

Memory and string functions, and the formatting core.

#include <string.h>

Types

Type Definition Description
strfmt_outc_t void(*) Character sink for vstrfmt(): receives ctx and each output character.

Constants

Name Value Description
align_down(addr, align) Rounds addr down to a multiple of align, a power of two.
align_up(addr, align) Rounds addr up to a multiple of align, a power of two.

Functions

memcpy

void * memcpy(void *dest, const void *src, size_t n);

Copy n bytes from source to destination.

Parameter Direction Description
dest out Pointer to the destination memory area.
src in Pointer to the source memory area; must not overlap dest.
n in Number of bytes to copy.

Returns: Pointer to the destination memory area.

memset

void * memset(void *dst, int c, size_t n);

Set n bytes of memory to a specified value.

Parameter Direction Description
dst out Pointer to the memory area to set.
c in Value to set (interpreted as an unsigned char).
n in Number of bytes to set.

Returns: Pointer to the memory area.

memmove

void * memmove(void *dest, const void *src, size_t n);

Move n bytes from source to destination, handling overlapping regions.

Parameter Direction Description
dest out Pointer to the destination memory area.
src in Pointer to the source memory area.
n in Number of bytes to move.

Returns: Pointer to the destination memory area.

memcmp

int memcmp(const void *str1, const void *str2, size_t count);

Compare two memory areas byte by byte.

Parameter Direction Description
str1 in Pointer to the first memory area.
str2 in Pointer to the second memory area.
count in Number of bytes to compare.

Returns: An integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.

memchr

void * memchr(const void *s, int c, size_t n);

Scan a memory area for the first occurrence of a specified byte.

Parameter Direction Description
s in Pointer to the memory area to scan.
c in Byte value to search for (interpreted as an unsigned char).
n in Number of bytes to scan.

Returns: Pointer to the byte, or NULL if it does not occur in the first n bytes.

strlen

size_t strlen(const char *s);

Calculate the length of a null-terminated string.

Parameter Direction Description
s in Pointer to the input string.

Returns: Number of characters in the string (excluding the null terminator).

strnlen

size_t strnlen(const char *s, size_t maxlen);

Calculate the length of a string up to a maximum number of characters.

Parameter Direction Description
s in Pointer to the input string.
maxlen in Maximum number of characters to examine.

Returns: Number of characters in the string, at most maxlen (excluding the null terminator).

strcpy

char * strcpy(char *dest, const char *src);

Copy a string from src to dest.

Parameter Direction Description
dest out Destination buffer, large enough for src and its NUL.
src in Source string.

Returns: Pointer to dest.

strncpy

char * strncpy(char *dest, const char *src, size_t n);

Copy at most n characters of a string, padding with NULs up to n.

The result is not NUL-terminated if src has n or more characters.

Parameter Direction Description
dest out Destination buffer of at least n bytes.
src in Source string.
n in Bytes to write.

Returns: Pointer to dest.

strcmp

int strcmp(const char *s1, const char *s2);

Compare two strings lexicographically.

Parameter Direction Description
s1 in First string.
s2 in Second string.

Returns: 0 if equal, <0 if s10 if s1>s2.

strncmp

int strncmp(const char *s1, const char *s2, size_t n);

Compare up to n characters of two strings.

Parameter Direction Description
s1 in First string.
s2 in Second string.
n in Maximum number of characters to compare.

Returns: 0 if equal, <0 if s10 if s1>s2.

strchr

char * strchr(const char *s, int c);

Locate the first occurrence of a character in a string.

Parameter Direction Description
s in Pointer to the input string.
c in Character to locate (as an int, but treated as char).

Returns: Pointer to the first occurrence of c in s, or NULL if not found.

strrchr

char * strrchr(const char *s, int c);

Locate the last occurrence of a character in a string.

Parameter Direction Description
s in Pointer to the input string.
c in Character to locate (as an int, but treated as char).

Returns: Pointer to the last occurrence of c in s, or NULL if not found.

vstrfmt

void vstrfmt(strfmt_outc_t outc, void *ctx, const char *fstring, va_list *args);

Format a string according to a format specifier list using va_list.

Supports format specifiers: c, s, d, i, u, x, X, o, b, p, P, %%

Length modifiers: hh, h, l, ll, z (for size_t/ptrdiff_t) Flags: -, 0, +, space, # Width and precision are supported.

Examples: zu (unsigned size_t), zd (signed size_t), zx (size_t in hex)

Parameter Direction Description
outc in Function pointer to output a single character.
ctx in Context pointer passed to outc.
fstring in Format string.
args inout Pointer to va_list of arguments matching the format specifiers.

vsnprintf

int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);

Bounded formatted output into a buffer; documented in snprintf.h.

snprintf

int snprintf(char *buf, size_t size, const char *fmt,...);

Bounded formatted output into a buffer; documented in snprintf.h.