| by Arround The Web | No comments

Strcpy() Function in C Language

The C standard library defines a large number of functions to handle the strings in the “string.h” header. The functions that are defined there can copy, concatenate, split, or perform any other process on a given string.

The knowledge of the input arguments and their data type as well as the calling methods of these functions is important for the programmer since they’re widely used in most programs of the C language and require fluent handling.

In this Linuxhint article you’ll learn how to use the strcpy() function to copy the strings.
We’ll show you the syntax, the input arguments, and the data types that each of them accepts. Then, we’ll apply what we learned in practical examples that include the code snippets and images which show the use of strcpy() in different cases and with different calling methods.

Syntax of the Strcpy() Function in C Language

char *strcpy(char *s1, char *s2);

Description of the Strcpy() Function in C Language

The strcpy() function copies the string that is pointed to by “s2” to “s1”. As a result, it returns a pointer to the string with the new copy. The copy is a literal and contains control, special, and escape characters. Like all string processing functions, strcpy() inserts a null character after the last character of the new string.

The strcpy() function accepts a pointer, array, or an explicit string as an input argument to “s2”. The data type of the buffers that are used in its input and output arguments must be the pointers or arrays of elements of type char or unsigned char.

When using the strcpy() function, it’s important to allocate enough capacity to the buffers that you’ll use, taking into account the length of the strings that the program will process. If the size of the destination array is smaller than the size of the source string “s2”, an overflow or data truncation will occur.

The string handling functions, including strcpy(), are declared in the “string.h” header. To use them, you must include them in your code as follows:

#include <string.h>

How to Copy an Explicit String with the Strcpy() Function in C Language

In this example, we will look at how to copy an explicit string using the strcpy() function.
To do this, we insert the “stdio.h” and “string.h” headers into an empty “.c” file. Then, we open a main() function that returns empty. We declare the array dest with 50 elements of type char in it, where we store the copy.

After inserting the necessary headers and declaring the destination array, we call the strcpy() function and pass it as input arguments in “s2”, the explicit string that we want to copy. In this case, the classic phrase “Hello World” and the destination string dest in “s1”. Here is the code for this example:

#include <stdio.h>
#include <string.h>

void main ()
{
 char dest[50]="";
 strcpy( dest, "Hello World\n");
 printf ("%s", dest);
}

The following image shows the compilation and execution of this code:

How to Copy an Implicitly Declared String with the Strcpy() Function in C Language

In this example, we will show you how to use the strcpy() function to copy an implicit string. To do this, we add the headers that are used in the previous example and declare the destination array dest with 50 elements of type char inside the main() function and the pointer orig with the sentence “Today is a great day to learn programming” as a source array.

We then call the strcpy() function and pass the destination array dest as the first input argument and the pointer to the string orig as the second argument. We use the printf() function to display the contents of the new string in the command console.

#include <stdio.h>
#include <string.h>

void main ()
{
 char dest[50]="";
 char *orig;
 orig ="\nToday is a great day to learn programming\n";
 strcpy( dest, orig);
 printf ("%s", dest);
}

The following image shows the compilation and execution of this code. As we can see, the printf() function displays the contents of the dest string:

The new string can also be accessed via the pointer that is returned as the result of strcpy() as shown in the following:

#include <stdio.h>
#include <string.h>

void main ()
{
 char dest[50]="";
 char *orig;
 orig ="\nToday is a great day to learn programming\n";
 printf ("%s", strcpy( dest, orig));
}

How to Copy a Certain Number of Characters from a String with the Strncpy() Variant in C Language

The strncpy() function is a variant of strcpy() and is used in cases where only a specified number of characters are to be copied. This function takes three input arguments. The first two are the destination and source arrays, and the third is a size_t data type that specifies the number of characters to copy. The following code uses the strncpy() function to copy only the first 21 characters of the string:

#include <stdio.h>
#include <string.h>

void main ()

{
 char dest[50]="";
 char *orig;
 orig ="Today is a great day to learn programming\n";
 printf ("\n%s\n", strncpy( dest, orig, 21));
}

The following image shows the result of running this code. As we can see, strncpy() copies only the first 21 characters of the source string:

Conclusion

In this Linuxhint article, we explained how to use the strcpy() function to copy the strings. We showed you a theoretical description of the syntax, how it works, the input and output arguments, and the data types that are used by each one.

Share Button

Source: linuxhint.com

Leave a Reply