Skip to main content Link Menu Expand (external link) Document Search Copy Copied

strlcpy Function

C

Function

Reference manual

@see man strlcpy

The strlcpy function copies the content of one string src to another string dst while ensuring that the resulting string is null-terminated and does not exceed a specified buffer size. It returns the length of the source string.

Syntax

size_t strlcpy(char *dst, const char *src, size_t size);

Parameters

  • dst: The destination string where the content of src is copied.
  • src: The source string to be copied to dst.
  • size: The size of the buffer (including the null-terminator) to prevent buffer overflows.

Return value

Returns the length of the source string. If the buffer size is insufficient to accommodate the entire source string, it returns the required buffer size.

Example

In the following example we use the strlcpy function to copy the string “Hello, World!” to an empty string with insufficient space to hold the entire source string, then we output the destination string and the length it would need to fit the entire source string.

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

int main(void)
{
    char destination[10];
    const char *source = "Hello, World!";
    size_t source_length;

    source_length = strlcpy(destination, source, sizeof(destination));
    printf("Copied string: %s\n", destination);
    printf("Source length: %zu\n", source_length);
    return (0);
}