Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<string.h> — strings and memory

The <string.h> header provides operations on null-terminated strings and blocks of memory.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <string.h>

#include <string.h> provides string and memory functions, plus size_t and NULL. A C string ends with \0; strlen returns the number of preceding bytes, excluding the terminator.

memcpy copies a specified byte count; overlapping regions cause undefined behavior. Use memmove for overlap. The destination buffer must have sufficient capacity; copying a string requires accounting for its terminator.

In the example, sizeof source includes \0, so the copy is a valid string. strcmp compares strings: a negative, zero, or positive result means less than, equal to, or greater than, respectively; do not assume exactly -1 or 1.

Examples

#include <stdio.h>
#include <string.h>

int main(void)
{
    const char source[] = "cat";
    char copy[sizeof source];

    memcpy(copy, source, sizeof source);
    printf("%s: %zu\n", copy, strlen(copy));
    return 0;
}

Result

cat: 3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources