BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strcpy() Function – C tutorial

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: c-programming

The strcpy() function copies one string to another string.

C strcpy() function declaration

char *strcpy(char *str1, const char *str2)

str1 – This is the destination string where the value of other string str2 is copied. First argument in the function
str2 – This is the source string, the value of this string is copied to the destination string. This is the second argument of the function.

Return value of strcpy()

This function returns the pointer to the destination string or you can say that it returns the destination string str1.

Example: strcpy() function

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

int main () {
   char str1[20];
   char str2[20];

   //copying the string "Apple" to the str1
   strcpy(str1, "Apple");
   printf("String str1: %s\n", str1);

   //copying the string "Banana" to the str2
   strcpy(str2, "Banana");
   printf("String str2: %s\n", str2);

   //copying the value of str2 to the string str1
   strcpy(str1, str2); 
   printf("String str1: %s\n", str1);

   return 0;
}

Output:

String str1: Apple
String str2: Banana
String str1: Banana

As I have mentioned in the beginning of the post that this function returns the pointer to the destination string which means if we display the return value of the function that it should display the value of the destination string after copying the source string to it. Lets take an example to understand this.

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

int main () {
   char str[20];

   /* copying the string "Apple" to the str and
    * displaying the return value of strcpy()
    */
   printf("Return value of function: %s\n", strcpy(str, "Apple"));
   printf("String str1: %s\n", str);

   return 0;
}

Output:

Return value of function: Apple
String str1: Apple

Related Posts:

  1. C – strcat() example
  2. C – strchr() example
  3. C – strcmp() example
  4. C – strcoll() example

Top Related Articles:

  1. C – loops in C programming with examples
  2. C strcmp() Function with example
  3. C strcat() Function with example
  4. C strcoll() Function – C tutorial
  5. C strchr() Function with example

Tags: C-Library, C-String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap