[C] strcat, strncat, strlcat 차이점

숭글·2022년 4월 20일
0

In manual.

#include <string.h>

char *strcat(char *restrict dest, const char *restrict src);

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.
The strcat() and strncat() functions return a pointer to the resulting string dest.

dest의 뒤에 src 문자열을 붙여준다. src가 붙은 dest의 마지막에는 '\0'문자를 붙여준다. 리턴값은 dest의 시작 주소이다.

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

The strncat() function is similar, except that it will use at most n bytes from src; and src does not need to be null-terminated if it contains n or more bytes.
The strcat() and strncat() functions return a pointer to the resulting string dest.

strcat과 비슷하지만 오직 n바이트 만큼만 src에서 가져와 dest의 뒤에 붙인다. 리턴값도 같다.
만약 src의 길이가 명시해준 n보다 크다면 dest의 뒤에는 n + 1바이트만 붙인다(마지막에 '\0'문자). dest의 사이즈는 strlen(dest) + n + 1이상이어야한다.

int strlcat(char *dest, const char *src, size_t size);

The strlcat() function appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result.
strlcat() functions return the total length of the string they tried to create. For strlcpy() that means the length of src.

They are designed to be safer, more consistent, and less error prone replacements for strncat.
리턴값이 strcat, strncat과는 다르게 int 변수형이다. 붙이려고 했던 값을 리턴해주기 때문에 strlen(src)의 값을 리턴한다. 리턴값을 받으면 dest가 가져야하는 크기를 알 수 있다. src는 size - strlen(dest) - 1의 값만 dest의 뒤에 붙일 수 있다.


I'm trying to write all functions i implemented for studying.
It's important to make sure about the differences between similar functions to work exactly same with the original.
Sometimes, It's impressive about that it's been changed for having more secure, guaranteeing not to interrupt other memory.
I can learned from that effort many thing, and i will more!

profile
Hi!😁 I'm Soongle. Welcome to my Velog!!!

0개의 댓글