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
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ c-program-to-copy-string-string-copy-strcpy.aspx
C program to copy one string into another | Implementing strcpy() in C
In this program, we will read a string and copy the string into another using stringCopy() function which is implemented by own. #include <stdio.h> /******************************************************** * function name :stringCpy * Parameter :s1,s2 : string * Description : copies string ...
๐ŸŒ
CodeScracker
codescracker.com โ€บ c โ€บ program โ€บ c-program-copy-string.htm
C Program to Copy One String to Another
Now copy the string into another variable, say str2, using the strcpy() function. The strcpy() function takes two arguments. The first argument is the target variable where the string is going to be copied. The second argument is the source variable. The value of this variable is initialized ...
Top answer
1 of 2
2

You can use pointer arithmetic and the function memcpy:

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

int main( void )
{
    char str1[] = "123copy321";
    char str2[5];

    //copy str1[3] up to and including str1[6] to str2
    memcpy( str2, str1 + 3, 4 );

    //add terminating null character to str2
    str2[4] = '\0';

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

This program has the following output:

123copy321
copy
2 of 2
0

With theFunctionINeed(str1, str2, 3, 6); there are a number of issues:

  1. Source string may be less than 3.

  2. Available sub-string length may be less than 4.

  3. Destination array may be too small.

  4. Unusual to pass in the first and last index to copy. This prevents forming a zero-length sub-string. More idiomatic to pass in beginning and 1) length or 2) index of one-past.

  5. How about returning something useful, like was the destination big enough?

Alternative untested sample code follows. restrict means the two pointers should not point to overlapping memory.

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

// Return `destination` when large enough
// otherwise return NULL when `size` was too small. 
bool SubString(size_t destination_size, char *restrict destination,
    const char *restrict source, size_t offset, size_t length) {
  if (destination_size == 0) {
    return NULL;
  }
  destination[0] = '\0';

  // Quickly search for the null character among the first `offset` characters of the source.
  if (memchr(source, '\0', offset)) {
    return destination;
  }

  destination_size--;
  size_t destination_length = length <= destination_size ? length : destination_size;
  strncat(destination, source + offset, destination_length);
  return length <= destination_size ? destination : NULL;
}
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ string โ€บ c-string-exercise-8.php
C Program: Copy one string into another string - w3resource
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str1[100], str2[100]; // Declare two character arrays to store strings int i; // Declare a variable for iteration printf("\n\nCopy one string into another string :\n"); // Display information about the task printf("-----------------------------------------\n"); printf("Input the string : "); fgets(str1, sizeof str1, stdin); // Read a string from the standard input (keyboard) /* Copies string1 to string2 character by character */ i = 0; // Initialize the iteration variable while (str1[i] != '\0') { // Loop until the en
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to copy one string to another string
C program to copy one string to another string - Codeforwin
July 20, 2025 - In C programming, NULL character is represented with 0. Hence, we can embed the string copy logic text2[i] = text1[i] in the while loop condition. Means, you can also write the above while loop as while(text2[i] = text1[++i]);. This will copy ...
๐ŸŒ
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. String s2: Hey fellow programmer. The above program copies the content of string s1 to string s2 manually.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
Answer (1 of 2): Three major ways. 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 up to a given ...
Find elsewhere
๐ŸŒ
Learn Java
javatutoring.com โ€บ c-program-to-copy-one-string-to-another
C Program To Copy One String To Another String | 4 Simple Ways
January 11, 2026 - The main() function calls the stringcopy() function by passing s1,s2 as arguments. 2) The function stringcopy() will copy the one string elements into the another string.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-copy-a-string-using-strcpy-function-in-c
How to copy a string using strcpy() function in C
strcpy() takes two strings as arguments and character by character (including \0) copies the content of string Src to string Dest, character by character.
๐ŸŒ
YouTube
youtube.com โ€บ watch
c program to copy one string into another | strcpy() function in c - YouTube
What is String? With Example Program...!๐Ÿ‘‡https://www.youtube.com/playlist?list=PLqleLpAMfxGAIBEDg0mvnCbS8nEvwWbUqPlease Subscribe our Channel...!Learn Codin...
Published ย  November 5, 2019
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ recursion โ€บ c-recursion-exercise-19.php
C Program: Copy One string to another - w3resource
November 1, 2025 - The first string is : w3resource The copied string is : w3resource ... void copyString(char stng1[], char stng2[], int ctr) { stng2[ctr] = stng1[ctr]; if (stng1[ctr] == '\0') return; copyString(stng1, stng2, ctr + 1); }
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ all about c language โ€บ program for string copy in c
String Copy in C | PrepInsta
January 31, 2023 - ... Step 2: Declare the variable str1 and str2. Step 3: Read input str1 from the user or predefine it according to the need. Step 4: Use the syntax to copy the string into the empty string.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ string.h โ€บ strcpy
C strcpy() - C Standard Library
#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; }
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ how-to-copy-one-string-to-another-in-c
How to Copy One String to Another in C - Examples
February 20, 2025 - We declare another character array destination large enough to store the copied string. We use strcpy(destination, source) to copy the contents of source into destination. The printf() function prints the copied string.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-copy-string-using-recursion
C Program to Copy One String to Another using Recursion - Sanfoundry
May 13, 2022 - In this C Program, the character from str1 is being copied to str2 until null is encountered in the string. index is incremented by 1 to move the recursion call to next state. 4. While child function returning the control to parent function the character in str1 is copied to str2.
๐ŸŒ
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 - The easiest way to copy a string is to use the assignment operator (=) of the std::string class to copy the contents of one string to another.
๐ŸŒ
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 ...