C Online Compiler
Example: String Copy Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Copy Example #include <stdio.h> #include <string.h> // Required for strcpy() int main() { // Step 1: Declare a source string char source[] = "Hello World"; // Step 2: Declare a destination string with enough capacity // +1 for the null terminator. Ensure destination is large enough! char destination[20]; // Step 3: Copy source to destination using strcpy() strcpy(destination, source); // Step 4: Print both strings printf("Source string: %s\n", source); printf("Destination string: %s\n", destination); // Note: strcpy does not check buffer size. // If destination is smaller than source, it leads to a buffer overflow. // For safer copying, consider strncpy(). return 0; }
Output
Clear
ADVERTISEMENTS