You are on page 1of 9

Skip to content

 Tutorials
 Student
 Jobs
 Courses

Write
Come write articles for us and get featured

Practice
Learn and code with the best industry experts

Premium
Get access to ad-free content, doubt assistance and more!

Jobs
Come and find your dream job with us

o Geeks Digest
o Quizzes
o Geeks Campus
o Gblog Articles
o IDE
o Campus Mantri



 My Profile  Edit Profile

 My Courses  Go Premium

  Logout
 Home
 Courses

o Practice DS & Algo.
o Algorithms
o Analysis of Algorithms
o Data Structures
o Interview Corner
o Languages
o ISRO CS
o GATE
o CS Subjects
o Web Technologies
o School Learning
o Mathematics
o Maths Notes (Class 8-11)
o NCERT Solutions
o RD Sharma Solutions
o UGC NET CS
o Student
o Jobs
 GBlog
 Puzzles
 What's New ?
 Change Language

Related Articles

Related Articles
 A shorthand array notation in C for repeated values
 Accessing array out of bounds in C/C++
 strcpy in C/C++
 strcmp() in C/C++
 Comparing two strings in C++
 std::string::compare() in C++
 Comparator function of qsort() in C
 std::sort() in C++ STL
 What are the default values of static variables in C?
 Understanding “volatile” qualifier in C | Set 2 (Examples)
 Const Qualifier in C
 Initialization of static variables in C
 Understanding “register” keyword in C
 Understanding “extern” keyword in C
 Storage Classes in C
 Static Variables in C
 Memory Layout of C Programs
 How to deallocate memory without using free() in C?
 Difference Between malloc() and calloc() with Examples
 Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
 How to dynamically allocate a 2D array in C?
 How to pass a 2D array as a parameter in C?
 Multidimensional Arrays in C / C++
 2D Vector In C++ With User Defined Size
 Vector of Vectors in C++ STL with Examples
 What is Memory Leak? How can we avoid?
 Arrays in C/C++
 Bitwise Operators in C/C++

strcpy in C/C++
 Difficulty Level : Easy
 Last Updated : 21 Jan, 2021
strcpy() is a standard library function in C/C++ and is used to copy one string to
another. In C it is present in string.h header file and in C++ it is present
in cstring header file. 
Syntax: 
char* strcpy(char* dest, const char* src);
Parameters: This method accepts the following parameters:  
 dest: Pointer to the destination array where the content is to be copied.
 src: string which will be copied.
Return Value: After copying the source string to the destination string, the strcpy()
function returns a pointer to the destination string.
Below program explains different usages of this library function:  

// C program to illustrate

// strcpy() function ic C/C++

#include<stdio.h>

#include<string.h>

 
int main ()

    char str1[]="Hello Geeks!";

    char str2[] = "GeeksforGeeks";

    char str3[40];

    char str4[40];

    char str5[] = "GfG";

     

    strcpy(str2, str1);

    strcpy(str3, "Copy successful");

    strcpy(str4, str5);

    printf ("str1: %s\nstr2: %s\nstr3: %s\nstr4:

                  %s\n", str1, str2, str3, str4);

    return 0;

Output: 
str1: Hello Geeks!
str2: Hello Geeks!
str3: Copy successful
str4: GfG
Important Points 
 This function copies the entire string to the destination string. It doesn’t append
the source string to the destination string. In other words, we can say that it
replaces the content of destination string by the content of source string.
 It does not affect the source string. The source string remains same after
copying.
 This function only works with C style strings and not C++ style strings i.e. it
only works with strings of type char str[]; and not string s1; which are created
using standard string data type available in C++ and not C.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would
like to contribute, you can also write an article using contribute.geeksforgeeks.org or
mail your article to contribute@geeksforgeeks.org. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above. 
Want to learn from the best curated videos and practice problems, check out the C
Foundation Course for Basic to Advanced C.
Like37
Previous
Accessing array out of bounds in C/C++
Next
strcmp() in C/C++
ADVERTISEMENT BY ADRECOVER

RECOMMENDED ARTICLES
Page :

1
2

C program to copy string without using strcpy() function

28, Nov 17

Why strcpy and strncpy are not safe to use?

03, Aug 18

Program to perform a letter frequency attack on a monoalphabetic substitution cipher

22, Jul 21
Implementing Sets Without C++ STL Containers

20, Jul 21

C/C++ program to implement the cricket game

20, Jul 21

How to create Binary File from the existing Text File?

20, Jul 21

Program that allows integer input only

20, Jul 21

Map Policy Based Data Structure in g++

19, Jul 21

Recursive lambda expressions in C++


19, Jul 21

Object Delegation in C++


19, Jul 21

Creating a C++ reusable Header File and its Implementation Files


19, Jul 21
C Library Functions
18, Jul 21

Article Contributed By :

GeeksforGeeks
Vote for difficulty

Current difficulty : Easy


EasyNormalMediumHardExpert

Improved By :

 oliverelarsen
 CoderSaty
Article Tags :

 C-String
 cpp-string
 C Language
 C++
Practice Tags :

 CPP
Improve Article

Report Issue

ADS BY ADRECOVER

WHAT'S NEW

Ad free experience with GeeksforGeeks Premium

View Details
DSA Self Paced Course

View Details

DSA Live Classes for Working Professionals

View Details

ADS BY ADRECOVER

MOST POPULAR IN C LANGUAGE


 Converting Strings to Numbers in C/C++
 Core Dump (Segmentation fault) in C/C++
 Left Shift and Right Shift Operators in C/C++
 Substring in C++
 Structures in C
ADS BY ADRECOVER

MOST VISITED IN C++


 Vector in C++ STL
 The C++ Standard Template Library (STL)
 Map in C++ Standard Template Library (STL)
 Object Oriented Programming in C++
 Inheritance in C++
ADS BY ADRECOVER

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

ADVERTISEMENT BY ADRECOVER

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org
 Company
 About Us
 Careers
 Privacy Policy
 Contact Us
 Copyright Policy
 Learn
 Algorithms
 Data Structures
 Languages
 CS Subjects
 Video Tutorials
 Practice
 Courses
 Company-wise
 Topic-wise
 How to begin?
 Contribute
 Write an Article
 Write Interview Experience
 Internships
 Videos
@geeksforgeeks , Some rights reserved

You might also like