totn C Functions

C Language: strcpy function
(String Copy)

In the C Programming Language, the strcpy function copies the string pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination.

Syntax

The syntax for the strcpy function in the C Language is:

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

Parameters or Arguments

s1
An array where s2 will be copied to.
s2
The string to be copied.

Returns

The strcpy function returns s1.

Required Header

In the C Language, the required header for the strcpy function is:

#include <string.h>

Applies To

In the C Language, the strcpy function can be used in the following versions:

  • ANSI/ISO 9899-1990

strcpy Example

Let's look at an example to see how you would use the strcpy function in a C program:

/* Example using strcpy by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{
    /* Create an example variable capable of holding 50 characters */
    char example[50];

    /* Copy the string "TechOnTheNet.com knows strcpy!" into the example variable */
    strcpy (example, "TechOnTheNet.com knows strcpy!");

    /* Display the contents of the example variable to the screen */
    printf("%s\n", example);

    return 0;
}

When compiled and run, this application will output:

TechOnTheNet.com knows strcpy!

Similar Functions

Other C functions that are similar to the strcpy function: