A C string is a nul-terminated character array.

The C language does not allow assigning the contents of an array to another
array. As noted by Barry, you must copy the individual characters one by one
from the source array to the destination array. e.g. -

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 

int main()
{
    char str1[] = "Hello";
    char str2[10] = {0};

    for (int x = 0; x < strlen(str1); ++x)
    {
        str2[x] = str1[x];
    }

    printf("%s\n", str2);

    return 0;
}

To make this common task easier there are standard library functions provided
which will perform this operation. e.g. - memcpy(), etc.

memcpy(str2, str1, 6);

When the array contains a nul-terminated string of characters you can use
strcpy(), etc.

strcpy(str2, str1);

Caveat: Some of the above functions are considered unsafe as they do not guard
against buffer overruns of the source and destination arrays. There are safer
versions provided by the compiler.

Note that if and when you start learning C++ you will find that there you can
assign a C++ std::string object to another object of the same type. However,
even in C++ the same rules apply when working with C strings, "raw" character
arrays, etc.

  • Wayne
Answer from WayneAKing on learn.microsoft.com
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcpy-in-c
strcpy() in C - GeeksforGeeks
March 6, 2026 - It is a part of the C standard strings library. The strcpy function in C is used to copy a string, with syntax: strcpy(dest, src);, where dest is the destination array and src is the source string.
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcpy
C strcpy() - C Standard Library
The strcpy() function also returns the copied string. The strcpy() function is defined in the string.h header file. #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; }
Discussions

Copy string in C
A C string is a nul-terminated character array. The C language does not allow assigning the contents of an array to another array. As noted by Barry, you must copy the individual characters one by one from the source array to the destination array. More on learn.microsoft.com
🌐 learn.microsoft.com
3
0
September 12, 2020
c++ - Proper way to copy C strings - Stack Overflow
You're probably looking for strncpy, ... first n characters from a string. Just be sure to add the null-terminator at position n of the copied-to string. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Not exhaustive pattern matching in Java. When it should be · How to colorized any line drawed with draw (nor with plot expression)? What's the first sci-fi story where a pet has an important role? Examples of cocomplete ... More on stackoverflow.com
🌐 stackoverflow.com
Implementing a string copy function in C - Stack Overflow
I looked at some other examples online and found that since all strings in C are null-terminated, I should have read up to the null character and then appended a null character to the destination string before exiting. However one thing I'm curious about is how memory is being handled. I noticed if I used the strcpy() library function, I could copy ... More on stackoverflow.com
🌐 stackoverflow.com
Safest way to copy a string?
One option is snprintf(dest,n,“%s”,src), but this will likely be a little slower due to the time needed to parse the format string. More on reddit.com
🌐 r/C_Programming
79
55
May 7, 2023
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strcpy.php
C Language: strcpy function (String Copy)
The string to be copied. The strcpy function returns s1. In the C Language, the required header for the strcpy function is: ... /* 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; }
🌐
W3Schools
w3schools.com › c › ref_string_strcpy.php
C string strcpy() Function
C Examples C Real-Life Examples ... C Study Plan C Interview Q&A ... char str1[] = "Hello World!"; char str2[30]; strcpy(str2, str1); printf("%s\n", str1); printf("%s\n", str2); Try it Yourself » · The strcpy() function ...
🌐
Programiz
programiz.com › c-programming › examples › string-copy
C Program to Copy String Without Using strcpy()
#include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); fgets(s1, sizeof(s1), stdin); for (i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); return 0; } ... Enter string s1: Hey fellow programmer.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › different-ways-to-copy-a-string-in-c-c
Different ways to copy a string in C/C++ - GeeksforGeeks
July 23, 2025 - We can use the inbuilt function strcpy() from <string.h> header file to copy one string to the other.
Find elsewhere
Top answer
1 of 4
30

You could use strdup() to return a copy of a C-string, as in:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

stringB = strdup(stringA);
/* ... */
free(stringB);
stringB = NULL; 

You could also use strcpy(), but you need to allocate space first, which isn't hard to do but can lead to an overflow error, if not done correctly:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strcpy( stringB, stringA );
/* ... */
free(stringB);
stringB = NULL;

If you cannot use strdup(), I would recommend the use of strncpy() instead of strcpy(). The strncpy() function copies up to — and only up to — n bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n, however, you would need to terminate stringB, yourself. But, generally, you'll know what sizes you need for things:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strncpy( stringB, stringA, strlen(stringA) + 1 );
/* ... */
free(stringB);
stringB = NULL;

I think strdup() is cleaner, myself, so I try to use it where working with strings exclusively. I don't know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.

Note that I cast the result of malloc() to char *. This is because your question is tagged as a c++ question. In C++, it is required to cast the result from malloc(). In C, however, you would not cast this.

EDIT

There you go, there's one complication: strdup() is not in C or C++. So use strcpy() or strncp() with a pre-sized array or a malloc-ed pointer. It's a good habit to use strncp() instead of strcpy(), wherever you might use that function. It will help reduce the potential for errors.

2 of 4
4

If I just initialize stringB as char *stringB[23], because I know I'll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way?

Almost. In C, if you know for sure that the string will never be too long:

char stringB[MAX+1];
assert(strlen(stringA) <= MAX));
strcpy(stringB, stringA);

or, if there's a possibility that the string might be too long:

char stringB[MAX+1];
strncpy(stringB, stringA, MAX+1);
if (stringB[MAX] != '\0') {
    // ERROR: stringA was too long.
    stringB[MAX] = '\0'; // if you want to use the truncated string
}

In C++, you should use std::string, unless you've proved that the overhead is prohibitive. Many implementations have a "short string optimisation", which will avoid dynamic allocation for short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i] gives the character at position i as an lvalue. Copying becomes stringB = stringA; with no danger of undefined behaviour.

If you really do find that std::string is unusable, consider std::array<char,MAX+1>: a copyable class containing a fixed-size array.

If stringB is checked for equality with other C-strings, will the extra space affect anything?

If you use strcmp, then it will stop at the end of the shortest string, and will not be affected by the extra space.

🌐
Weber State University
icarus.cs.weber.edu › ~dab › cs1410 › textbook › 8.Strings › strcpy.html
8.2.2.2. strcpy
For example, the null terminator in s2[5] overwrites the 'L' in s1[5], making the 'E' and the null terminator at positions 6 and 7 in s1 irrelevant because after the copy operation, s1 logically ends with the null terminator at position 5. The strcpy function is unusual in that it is not necessary to null-terminate the destination, s1 in the example, before calling the function. ... Returns 0 when the copy succeeds; returns non-0 on failure. ... Common strcpy errors. char* does not allocate memory to store the copied C-string.
🌐
Programming Simplified
programmingsimplified.com › c › source-code › c-program-copy-strings
String copy in C | Programming Simplified
void copy_string(char d[], char s[]) { int c = 0; while (s[c] != '\0') { d[c] = s[c]; c++; } d[c] = '\0'; }
🌐
ZetCode
zetcode.com › clang › strcpy
C strcpy Tutorial: String Copying with Practical Examples
It's declared in string.h and takes two parameters: destination and source pointers. strcpy copies until it encounters the null terminator. It doesn't check for buffer sizes, making it potentially unsafe. For security-critical code, consider strncpy or strlcpy for bounds-checked copying. This example demonstrates copying a string using strcpy.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strcpy.htm
C library - strcpy() function
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("The result of copied string: %s\n", destination); return 0; }
🌐
Cplusplus
cplusplus.com › reference › cstring › strcpy
strcpy - cstring
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcpy in c
Strcpy in C: Copy Strings Safely with Examples
January 4, 2026 - ... #include <stdio.h> #include <string.h> int main() { char source[] = "This string is too long!"; char destination[10]; // Not enough space strcpy(destination, source); // Unsafe operation printf("Copied String: %s\n", destination); return 0; }
🌐
W3Schools
w3schools.in › c-programming › examples › copy-string
C Program to Copy String Using strcpy - W3Schools
#include<stdio.h> #include<string.h> main() { char source[] = "C Program"; char destination[50]; strcpy(destination, source); printf("Source string: %s\n", source); printf("Destination string: %s\n", destination); return 0; } ... This program is used to copy a character array's value from one ...
🌐
Polytechnique
lix.polytechnique.fr › ~liberti › public › computing › prog › c › C › FUNCTIONS › strcpy.html
strcpy function - LIX
Consider this piece of code. main() { char *string2="red dwarf"; char *string1; string1=string2; } 'string2' is now a character pointer (only one byte) that points to a storage location containing "red dwarf" (a string constant). So string1=string2; copies the address of "red dwarf" into 'string1'.
🌐
W3Resource
w3resource.com › c-programming › string › c-strcpy.php
C strcpy() function
#include <stdio.h> #include <string.h> ... char name[] = "Gamil"; // Copy the name into the greeting after "Hello, " strcpy(greeting + 7, name); // Print the complete greeting printf("Full Greeting: %s\n", greeting); return ...
🌐
Reddit
reddit.com › r/c_programming › safest way to copy a string?
r/C_Programming on Reddit: Safest way to copy a string?
May 7, 2023 -

I just fell foul of the fact that strncpy does not add an old terminator if the destination buffer is shorter than the source string. Is there a single function standard library replacement that I could drop in to the various places strncpy is used that would copy a null terminated string up to the length of the destination buffer, guaranteeing early (but correct) termination of the destination string, if the destination buffer is too short?

Edit:

  • Yes, I do need C-null terminated strings. This C API is called by something else that provides a buffer for me to copy into, with the expectation that it’s null terminated

Edit 2:

  • I know I can write a helper function that’s shared across relevant parts of the code, but I don’t want to do that because then each of those modules that need the function becomes coupled to a shared helper header file, which is fine in isolation but “oh I want to use this code in another project, better make sure I take all the misc dependencies” is best avoided. Necessary if necessary, but if possible using a standard function, even better.

🌐
Quora
quora.com › How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
1. strncpy [code]char* src = "a const string to be copied"; char dest[28] = {0}; char *strncpy(char *dest, const char *src, size_t n); dest[n]= '\0'; // terminate manually [/code] 1. strncpy copies a char array src into another char array dest ...