Syntax
#include <string.h> char *strcpy(char *string1, const char *string2);Description
strcpy copies string2, including the ending null character, to the location specified by string1.
strcpy operates on null-terminated strings. The string arguments to the function should contain a null character (\0) marking the end of the string. No length checking is performed. You should not use a literal string for a string1 value, although string2 may be a literal string.
strcpy returns a pointer to the copied string (string1.).
This example copies the contents of source to destination.
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char source[SIZE] = "123456789";
char source1[SIZE] = "123456789";
char destination[SIZE] = "abcdefg";
char destination1[SIZE] = "abcdefg";
char *return_string;
int index = 5;
/* This is how strcpy works */
printf("destination is originally = '%s'\n", destination);
return_string = strcpy(destination, source);
printf("After strcpy, destination becomes '%s'\n\n", destination);
/* This is how strncpy works */
printf("destination1 is originally = '%s'\n", destination1);
return_string = strncpy(destination1, source1, index);
printf("After strncpy, destination1 becomes '%s'\n", destination1);
return 0;
/****************************************************************************
The output should be:
destination is originally = 'abcdefg'
After strcpy, destination becomes '123456789'
destination1 is originally = 'abcdefg'
After strncpy, destination1 becomes '12345fg'
****************************************************************************/
}
Related Information