Good interview question has several layers, to which to candidate can demonstrate different levels of understanding.

On the syntactic 'C language' layer, the following code is from the classic Kernighan and Ritchie book ('The C programming language'):

while( *dest++ = *src++ )
    ;

In an interview, you could indeed point out the function isn't safe, most notably the buffer on *dest isn't large enough. Also, there may be overlap, i.e. if dest points to the middle of the src buffer, you'll have endless loop (which will eventually creates memory access fault).

Answer from Uri London on Stack Overflow
🌐
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

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
String builders in C
They aren’t standard (yet), but widely available are fmemopen and open_memstream. And asprintf can handle simpler cases. It’s worth familiarizing yourself with all the dynamic memory extensions functions and checking to see if they’re already available on your platform. More on reddit.com
🌐 r/C_Programming
9
19
June 3, 2023
🌐
W3Schools
w3schools.com › c › ref_string_strcpy.php
C string strcpy() Function
C Examples C Real-Life Examples ... str1); printf("%s\n", str2); Try it Yourself » · The strcpy() function copies data from one string into the memory of another 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'; }
🌐
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).
🌐
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 idea is to use a while loop to assign the content of string array1 to string array2 one by one and increment using the post-increment operator to move to the next character. The loop continues until the null character '\0' is found.
Find elsewhere
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strcpy.php
C Language: strcpy function (String Copy)
/* 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; }
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strcpy.htm
C library - strcpy() function
Below the example shows the usage of custom string copy without the use of strcpy(). #include <stdio.h> void customStrcpy(char* dest, const char* src) { while (*src) { *dest = *src; dest++; src++; } *dest = '\0'; } int main() { char source[] = "string Copy!"; char destination[20]; customStrcpy(destination, source); printf("The custom copied string: %s\n", destination); return 0; }
🌐
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.
🌐
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.
🌐
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 ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcpy in c
Strcpy in C: Copy Strings Safely with Examples
January 4, 2026 - Copied String: Hello, World! ... strcpy(destination, source); – Copies all characters from source to destination, including the null character.
🌐
Weber State University
icarus.cs.weber.edu › ~dab › cs1410 › textbook › 8.Strings › strcpy.html
8.2.2.2. strcpy
The compiler translates the first two statements into just a few, efficient machine instructions, making this an efficient implementation of the strcpy function. The assignment operation preserves the address stored in dest for statement (iii), allowing the loop to increment s without altering dest. The placement of the semicolon ending the while-loop denotes a beneficial null statement, so the character copy occurs inside the loop's test. The loop begins with the first character of each string, advances each pointer one character at a time by incrementing the addresses in each pointer, and copies the characters using the assignment operator.
🌐
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.

🌐
W3Schools
w3schools.in › c-programming › examples › copy-string
C Program to Copy String Using strcpy - W3Schools
This C program is used to copy the string by using the library function strcpy(). ... #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); ...
🌐
DEV Community
dev.to › sjmulder › string-copy-in-c-38k9
String copy in C - DEV Community
August 12, 2023 - Instead, use strcpy_s() if available, strclpy(), or even snprintf() (snprintf(dst, sizeof(dst), "%s", src)). ... void strcpy_1(char *src, char *dst) { size_t len, i; len = strlen(src); for (i=0; i < len; i++) dst[i] = src[i]; dst[len] = '\0'; ...
🌐
C For Dummies
c-for-dummies.com › blog
To Copy or to Duplicate a String | C For Dummies Blog
December 26, 2015 - The destination’s allocated space must be of the same size (or larger) than the source. The value returned is a pointer to the dst string. The strcpy() function duplicates string scr to the location specified by dst one character at a time until and including the null character, \0.
🌐
Scaler
scaler.com › home › topics › c strcpy()
C strcpy() - Scaler Topics
July 14, 2024 - The strcpy() in C, doesn't perform any comparison of the length of the two strings given. So the length of the destination string(character array) must be greater or equal to the length of the source string(character array). Suppose in any situation you want to copy the content of one string to another string without deleting the original string.