To use the strcpy in C, you need to first use header file #include and then you will need two strings (one from which the characters will be copied and one on which the characters will be copied). It returns the pointer to the destination string. If we display the return value of the strcpy() function, then the value of the destination string(character array) will be displayed in which the value of the source string(character array) was copied. Return Values strcpy() in CĪfter copying the source string to the destination string, the strcpy() function returns the destination string(which will have the content of the source string). The two strings will be the source string and the destination string so that the content of the source string can be copied to the destination string. In C to use the strcpy() function, we must pass two strings of the same length or the destination string should have greater length than the source string, as a parameter of the function. The strcpy() in C will not check If the source string(character array) will fit inside the destination string(character array) and it will lead to undefined behaviour. And as the loop finds the null character('\0'), the loop terminates.Īs it was mentioned earlier in the article, in strcpy() in C, either the destination string(character array) should have the greater size or both source and destination should have the same size. Initially, string2 is empty and string1 has content.Īfter we apply strcpy() in C, the content of string1 starts to be copied to string2.Īs every string is terminated with a null character ('\0'), The strcpy() in C runs a while loop until it finds the null character('\0') and inside the while loop the content of the source string(character array) is copied to the destination string(character array) in character by character. The content of String1 will be copied to string2. The image below explains the working of the strcpy() in C. We cannot change or modify the content of source string. Here, string 1 is the source (original) string from which the content will be copied and string 2 is the destination string where the content of string 1 will be pasted. The above code will copy the content of string1 to string2. It will save your time and the complexity of the code. You can use the strcpy() function instead of any looping statements. Suppose, in any situation you want to copy the content of one string to another string without deleting the original string. So the length of the destination string(character array) must be greater or equal to the length of the source string(character array). The strcpy() in C doesn't perform any comparison of the length of the two strings given. Or in easy terms, it copies the source string(character array) to the destination string(character array). It is used to copy the character array pointed by the source to the location pointed by the destination. The strcpy() is a library function available in string library in C. To perform string copying operation using the strcpy() function, we must add header file using #include. The strcpy() function doesn't create a string(character array), it only copies a string(character array). The strcpy() is a library function available in the string library in C and is used to copy the content of one string to another.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |