Question #5782   Submitted by Answiki on 01/23/2022 at 05:31:34 PM UTC

In C, how does strlcat() work?

Answer   Submitted by Answiki on 01/23/2022 at 05:38:05 PM UTC

strlcat() is a function found in some implementations of the string.h library, but it is not a standard C function. To use this function, you must include this library:

#include <bsd/string.h>

And compile with the -lbsd option, for example:

clang-7 -pthread -lm -lbsd -o main main.c

Here is the prototype:

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

The function concatenates the strings dst and src, and places the result in the string dst. Unlike the strcat() function, strlcat() guarantees that the total length of the result string will be between the initial length of dst and size. The zero at the end of the string is included. This function is mainly used to avoid oversize of the character array. Let's consider the following strings:

char dst[100] = "1234";
char src[100] = "ABCD";

Here are some examples of strlcat():

// Retourne 8
// dst = 1234AB
// src = ABCD
strlcat(dst, src, 7);

// Retourne 6
// dst = 1234
// src = ABCD
strlcat(dst, src, 2)

Vous pouvez tester la fonction en ligne sur repl.it.

2 events in history
Answer by Answiki on 01/23/2022 at 05:38:05 PM

strlcat() is a function found in some implementations of the string.h library, but it is not a standard C function. To use this function, you must include this library:

#include <bsd/string.h>

And compile with the -lbsd option, for example:

clang-7 -pthread -lm -lbsd -o main main.c

Here is the prototype:

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

The function concatenates the strings dst and src, and places the result in the string dst. Unlike the strcat() function, strlcat() guarantees that the total length of the result string will be between the initial length of dst and size. The zero at the end of the string is included. This function is mainly used to avoid oversize of the character array. Let's consider the following strings:

char dst[100] = "1234";
char src[100] = "ABCD";

Here are some examples of strlcat():

// Retourne 8
// dst = 1234AB
// src = ABCD
strlcat(dst, src, 7);

// Retourne 6
// dst = 1234
// src = ABCD
strlcat(dst, src, 2)

Vous pouvez tester la fonction en ligne sur repl.it.

Question by Answiki 01/23/2022 at 05:31:34 PM
In C, how does strlcat() work?
# ID Query URL Count

Icons proudly provided by Friconix.