strcpy() Copy an entire string

strcpy() Copy an entire string

strcpy() copies src and its null terminator to dest. Ensure strlen(src)+1 bytes fit; unterminated sources or overlapping regions cause undefined behavior.

Categorystring.h strings
KindLibrary function
Header<string.h>
C standardC89 / C90

📝 Syntax

char *strcpy(char *dest, const char *src);

⚙️ Parameters

dest RequiredA writable destination array with capacity of at least strlen(src) + 1.
src RequiredThe null-terminated source string to copy.

Returns: Returns dest.

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

Hello, C!
⚠️ Warning strcpy() performs no bounds checking. dest must hold the source and its null terminator, and overlapping source and destination cause undefined behavior.
💡 Tip The example uses strlen(source) < sizeof destination to ensure there is room for the string including its null terminator.

📚 Related Tutorials

❓ FAQ

QHow can I prevent destination overflow with strcpy?
ABefore copying, verify that strlen(src) + 1 fits in dest and that src is genuinely null-terminated. Do not copy directly when the source cannot be validated.
QCan the source and destination of strcpy overlap?
ANo. Overlap causes undefined behavior. Use a function such as memmove() when bytes must be moved between overlapping regions.
QWhich is safer, strcpy or strncpy, and how should I choose between them?
ANeither is automatically safe. strcpy requires a destination large enough for the source and its terminator; strncpy limits the write to n characters, but truncation may leave the destination unterminated, and shorter sources cause zero padding. Choose according to whether you need fixed-size records, always validate capacity and termination, and do not treat strncpy as a general-purpose safe copy function.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏