In C, how does strlcat() work?
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.
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.
# | ID | Query | URL | Count |
---|