1. λμ£Όμ : λ¬Έμμ΄ λ³΅μ¬νκΈ°
λ¬Έμμ΄μ 볡μ¬νλ μμ
μ νλ‘κ·Έλλ°μμ μμ£Ό μ¬μ©λλ κΈ°λ₯ μ€ νλμ
λλ€. C μΈμ΄μμλ λ¬Έμμ΄μ 볡μ¬νλ λ°μ μ¬μ©λλ λ κ°μ§ ν¨μκ° μμ΅λλ€. μ΄λ² κΈμμλ strcpyμ strncpy ν¨μμ λν΄ μμλ³΄κ² μ΅λλ€.
2. strcpy ν¨μ μ¬μ©λ²
strcpy ν¨μλ λ¬Έμμ΄μ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strcpy ν¨μμ κΈ°λ³Έ μ¬μ©λ²μ
λλ€.
char* strcpy(char* destination, const char* source);
destination: 볡μ¬λ λ¬Έμμ΄μ μ μ₯ν λμ λ¬Έμμ΄ ν¬μΈν°source: 볡μ¬ν λ¬Έμμ΄ ν¬μΈν°
strcpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ 볡μ¬ν©λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
3. strncpy ν¨μ μ¬μ©λ²
strncpy ν¨μλ λ¬Έμμ΄μ μ§μ λ κΈΈμ΄λ§νΌ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strncpy ν¨μμ κΈ°λ³Έ μ¬μ©λ²μ
λλ€.
char* strncpy(char* destination, const char* source, size_t num);
destination: 볡μ¬λ λ¬Έμμ΄μ μ μ₯ν λμ λ¬Έμμ΄ ν¬μΈν°source: 볡μ¬ν λ¬Έμμ΄ ν¬μΈν°num: 볡μ¬ν μ΅λ λ¬Έμ μ
strncpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ μ΅λ num κ°μ λ¬Έμλ§νΌ 볡μ¬ν©λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
4. strcpy ν¨μ μμ
λ€μμ strcpy ν¨μλ₯Ό μ¬μ©νλ μμ μ½λμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[20];
strcpy(destination, source);
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ΄ μμ μμλ source λ¬Έμμ΄μ destinationμ 볡μ¬νκ³ κ·Έ κ²°κ³Όλ₯Ό μΆλ ₯ν©λλ€.
5. strncpy ν¨μ μμ
λ€μμ strncpy ν¨μλ₯Ό μ¬μ©νλ μμ μ½λμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[10];
strncpy(destination, source, sizeof(destination) - 1);
destination[sizeof(destination) - 1] = '\0';
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ΄ μμ μμλ source λ¬Έμμ΄μ destinationμ μ΅λ 9κ° λ¬Έμλ§νΌ 볡μ¬νκ³ μΆλ ₯ν©λλ€. 볡μ¬λ λ¬Έμμ΄μ κΈΈμ΄κ° destination λ°°μ΄μ ν¬κΈ°λ₯Ό μ΄κ³Όνμ§ μλλ‘ μ£Όμν΄μΌ ν©λλ€.
2. strcpy ν¨μ μ¬μ©λ²
strcpy ν¨μλ λ¬Έμμ΄μ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strcpy ν¨μμ κΈ°λ³Έ μ¬μ©λ²μ
λλ€.
char* strcpy(char* destination, const char* source);
strcpy ν¨μμ λ§€κ°λ³μλ λ€μκ³Ό κ°μ΅λλ€.
destination: 볡μ¬λ λ¬Έμμ΄μ μ μ₯ν λμ λ¬Έμμ΄ ν¬μΈν°source: 볡μ¬ν λ¬Έμμ΄ ν¬μΈν°
strcpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ 볡μ¬ν©λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
볡μ¬κ° μ΄λ£¨μ΄μ§λ λμ, destination λ¬Έμμ΄μ΄ μΆ©λΆν ν¬κΈ°λ₯Ό κ°μ§κ³ μμ΄μΌ ν©λλ€. λ§μ½ destination λ¬Έμμ΄μ ν¬κΈ°κ° source λ¬Έμμ΄μμ 볡μ¬λλ λ¬Έμμ΄μ ν¬κΈ°λ³΄λ€ μμ κ²½μ°μλ μκΈ°μΉ μμ λμμ΄ λ°μν μ μμ΅λλ€.
λν, destination λ¬Έμμ΄μ λ°λμ 볡μ¬λ λ¬Έμμ΄μ μ μ₯νκΈ°μ μΆ©λΆν ν¬κΈ°λ‘ μ μΈλμ΄ μμ΄μΌ ν©λλ€. κ·Έλ μ§ μμΌλ©΄ λ©λͺ¨λ¦¬ μ€λ²νλ‘μ°κ° λ°μν μ μμ΅λλ€.
μλλ strcpy ν¨μλ₯Ό μ¬μ©νλ μμ μ½λμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[20];
strcpy(destination, source);
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ μμ μμλ "Hello, world!" λ¬Έμμ΄μ sourceμ μ μ₯νκ³ , destinationμλ μΆ©λΆν ν¬κΈ°μ λ°°μ΄λ‘ μ μΈν©λλ€. κ·Έ ν, strcpy ν¨μλ₯Ό μ¬μ©νμ¬ sourceμμ destinationμΌλ‘ λ¬Έμμ΄μ 볡μ¬ν©λλ€. 볡μ¬λ λ¬Έμμ΄μ "Hello, world!" μ΄λ―λ‘ μ΄λ₯Ό μΆλ ₯ν©λλ€.
3. strncpy ν¨μ μ¬μ©λ²
strncpy ν¨μλ λ¬Έμμ΄μ μ§μ λ κΈΈμ΄λ§νΌ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strncpy ν¨μμ κΈ°λ³Έ μ¬μ©λ²μ
λλ€.
char* strncpy(char* destination, const char* source, size_t num);
strncpy ν¨μμ λ§€κ°λ³μλ λ€μκ³Ό κ°μ΅λλ€.
destination: 볡μ¬λ λ¬Έμμ΄μ μ μ₯ν λμ λ¬Έμμ΄ ν¬μΈν°source: 볡μ¬ν λ¬Έμμ΄ ν¬μΈν°num: 볡μ¬ν μ΅λ λ¬Έμ μ
strncpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ μ΅λ num κ°μ λ¬Έμλ§νΌ 볡μ¬ν©λλ€. λ§μ½ source λ¬Έμμ΄μ κΈΈμ΄κ° numλ³΄λ€ μμ κ²½μ°, λ¨λ 곡κ°μ λ λ¬Έμ('\0')λ‘ μ±μμ§λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
볡μ¬κ° μ΄λ£¨μ΄μ§λ λμ, destination λ¬Έμμ΄μ΄ μΆ©λΆν ν¬κΈ°λ₯Ό κ°μ§κ³ μμ΄μΌ ν©λλ€. λ§μ½ destination λ¬Έμμ΄μ ν¬κΈ°κ° numλ³΄λ€ μμ κ²½μ°μλ λ λ¬Έμλ₯Ό 볡μ¬νκΈ° μν΄ μΆκ° λ©λͺ¨λ¦¬ 곡κ°μ΄ νμνλ―λ‘, μκΈ°μΉ μμ λμμ΄ λ°μν μ μμ΅λλ€.
μλλ strncpy ν¨μλ₯Ό μ¬μ©νλ μμ μ½λμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[10];
strncpy(destination, source, sizeof(destination) - 1);
destination[sizeof(destination) - 1] = '\0';
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ μμ μμλ "Hello, world!" λ¬Έμμ΄μ sourceμ μ μ₯νκ³ , destinationμλ μΆ©λΆν ν¬κΈ°μ λ°°μ΄λ‘ μ μΈν©λλ€. κ·Έ ν, strncpy ν¨μλ₯Ό μ¬μ©νμ¬ sourceμμ destinationμΌλ‘ μ΅λ 9κ°μ λ¬Έμλ₯Ό 볡μ¬ν©λλ€. sizeof(destination) - 1μ destination λ°°μ΄μ ν¬κΈ°μμ λ λ¬Έμλ₯Ό μν 곡κ°μ 미리 μ μΈν κ°μ
λλ€. λ§μ§λ§μΌλ‘, 볡μ¬λ λ¬Έμμ΄μ μΆλ ₯νκΈ° μ μ μ§μ λ λ¬Έμλ₯Ό μΆκ°νμ¬ λ¬Έμμ΄μ μμ±μν΅λλ€. κ²°κ³Όμ μΌλ‘ destinationμλ "Hello, wo" λ¬Έμμ΄μ΄ 볡μ¬λμ΄ μΆλ ₯λ©λλ€.
4. strcpy ν¨μ μμ
strcpy ν¨μλ λ¬Έμμ΄μ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strcpy ν¨μμ μμ μ½λμ μ€ν κ²°κ³Όμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[20];
strcpy(destination, source);
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ΄ μμ μμλ "Hello, world!" λ¬Έμμ΄μ sourceμ μ μ₯νκ³ , destinationμλ μΆ©λΆν ν¬κΈ°μ λ°°μ΄λ‘ μ μΈν©λλ€. κ·Έ ν, strcpy ν¨μλ₯Ό μ¬μ©νμ¬ sourceμμ destinationμΌλ‘ λ¬Έμμ΄μ 볡μ¬ν©λλ€. 볡μ¬λ λ¬Έμμ΄μ "Hello, world!" μ΄λ―λ‘ μ΄λ₯Ό μΆλ ₯ν©λλ€.
strcpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ 볡μ¬ν©λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
볡μ¬κ° μ΄λ£¨μ΄μ§λ λμ, destination λ¬Έμμ΄μ΄ μΆ©λΆν ν¬κΈ°λ₯Ό κ°μ§κ³ μμ΄μΌ ν©λλ€. λ§μ½ destination λ¬Έμμ΄μ ν¬κΈ°κ° source λ¬Έμμ΄μμ 볡μ¬λλ λ¬Έμμ΄μ ν¬κΈ°λ³΄λ€ μμ κ²½μ°μλ μκΈ°μΉ μμ λμμ΄ λ°μν μ μμ΅λλ€.
λν, destination λ¬Έμμ΄μ λ°λμ 볡μ¬λ λ¬Έμμ΄μ μ μ₯νκΈ°μ μΆ©λΆν ν¬κΈ°λ‘ μ μΈλμ΄ μμ΄μΌ ν©λλ€. κ·Έλ μ§ μμΌλ©΄ λ©λͺ¨λ¦¬ μ€λ²νλ‘μ°κ° λ°μν μ μμ΅λλ€.
μ μμ λ₯Ό μ€ννλ©΄ λ€μκ³Ό κ°μ κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€.
볡μ¬λ λ¬Έμμ΄: Hello, world!μ½λμμ strcpy ν¨μλ₯Ό μ¬μ©νμ¬ source λ¬Έμμ΄μ destinationμ 볡μ¬νμκΈ° λλ¬Έμ, destinationμλ "Hello, world!" λ¬Έμμ΄μ΄ 볡μ¬λμ΄ μΆλ ₯λ©λλ€.
5. strncpy ν¨μ μμ
strncpy ν¨μλ λ¬Έμμ΄μ μ§μ λ κΈΈμ΄λ§νΌ 볡μ¬νλ κΈ°λ₯μ μ 곡ν©λλ€. μλλ strncpy ν¨μμ μμ μ½λμ μ€ν κ²°κ³Όμ
λλ€.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[10];
strncpy(destination, source, sizeof(destination) - 1);
destination[sizeof(destination) - 1] = '\0';
printf("볡μ¬λ λ¬Έμμ΄: %s\n", destination);
return 0;
}
μ΄ μμ μμλ "Hello, world!" λ¬Έμμ΄μ sourceμ μ μ₯νκ³ , destinationμλ μΆ©λΆν ν¬κΈ°μ λ°°μ΄λ‘ μ μΈν©λλ€. κ·Έ ν, strncpy ν¨μλ₯Ό μ¬μ©νμ¬ sourceμμ destinationμΌλ‘ μ΅λ 9κ°μ λ¬Έμλ₯Ό 볡μ¬ν©λλ€. sizeof(destination) - 1μ destination λ°°μ΄μ ν¬κΈ°μμ λ λ¬Έμλ₯Ό μν 곡κ°μ 미리 μ μΈν κ°μ
λλ€. λ§μ§λ§μΌλ‘, 볡μ¬λ λ¬Έμμ΄μ μΆλ ₯νκΈ° μ μ μ§μ λ λ¬Έμλ₯Ό μΆκ°νμ¬ λ¬Έμμ΄μ μμ±μν΅λλ€.
strncpy ν¨μλ sourceμμ μμλλ λ¬Έμμ΄μ destinationμ μ΅λ num κ°μ λ¬Έμλ§νΌ 볡μ¬ν©λλ€. λ§μ½ source λ¬Έμμ΄μ κΈΈμ΄κ° numλ³΄λ€ μμ κ²½μ°, λ¨λ 곡κ°μ λ λ¬Έμ('\0')λ‘ μ±μμ§λλ€. μ΄ ν¨μλ 볡μ¬λ destination λ¬Έμμ΄ ν¬μΈν°λ₯Ό λ°νν©λλ€.
볡μ¬κ° μ΄λ£¨μ΄μ§λ λμ, destination λ¬Έμμ΄μ΄ μΆ©λΆν ν¬κΈ°λ₯Ό κ°μ§κ³ μμ΄μΌ ν©λλ€. λ§μ½ destination λ¬Έμμ΄μ ν¬κΈ°κ° numλ³΄λ€ μμ κ²½μ°μλ λ λ¬Έμλ₯Ό 볡μ¬νκΈ° μν΄ μΆκ° λ©λͺ¨λ¦¬ 곡κ°μ΄ νμνλ―λ‘, μκΈ°μΉ μμ λμμ΄ λ°μν μ μμ΅λλ€.
μ μμ λ₯Ό μ€ννλ©΄ λ€μκ³Ό κ°μ κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€.
볡μ¬λ λ¬Έμμ΄: Hello, woμ½λμμ strncpy ν¨μλ₯Ό μ¬μ©νμ¬ source λ¬Έμμ΄μ destinationμ μ΅λ 9κ°μ λ¬Έμλ₯Ό 볡μ¬νμκΈ° λλ¬Έμ, destinationμλ "Hello, wo" λ¬Έμμ΄μ΄ 볡μ¬λμ΄ μΆλ ₯λ©λλ€. λ λ¬Έμλ₯Ό μ§μ μΆκ°νμ¬ λ¬Έμμ΄μ μμ±μμΌ°κΈ° λλ¬Έμ μΆλ ₯ κ²°κ³Όμμλ "Hello, wo"λ‘ λλλ λ¬Έμμ΄μ νμΈν μ μμ΅λλ€.
λκΈ