C_C++/C_라이브러리_함수

(C언어) strncpy: 지정된 크기만큼 문자열을 복사한다

고니자니 2023. 3. 7. 08:53
반응형
#include<string.h>
char *strncpy(char *dest, const char *str, size_t maxlen);

strncpy 함수는 지정된 크기만큼의 문자열을 다른 문자열에 복사합니다.

NULL 문자를 자동으로 덧붙이지 않기 때문에 필요하면 NULL 문자를 추가해야 합니다.

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() 
{
	char s[100];
	char s2[] = "string copy";
	strncpy(s, s2, 6);
	s[6] = '\0';
	printf("%s\n", s);
		
	return 0;
}

728x90
반응형