Popular Tutorials
Start Learning CCertification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C strcpy()
In this tutorial, you will learn to use the strcpy() function in C programming to copy strings (with the help of an example).
C strcpy()
The function prototype of strcpy() is:
char* strcpy(char* destination, const char* source);
- The
strcpy()function copies the string pointed by source (including the null character) to the destination. - The
strcpy()function also returns the copied string.
The strcpy() function is defined in the string.h header file.
Example: C strcpy()
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
Output
C programming
Note: When you use strcpy(), the size of the destination string should be large enough to store the copied string. Otherwise, it may result in undefined behavior.
Did you find this article helpful?