C strcpy() method

This function copies the contents of one string (source) to another string (destination).

Syntax:


strcpy(destination, source);

Explanation:

  • source: A pointer to the source string.
  • destination: A pointer to the destination string.

Note: To copy one string to another. Ensure that the destination has enough space to hold the source string.

Example:


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

int main() {
    char source[] = "My World";
    char destination[10];
    
    strcpy(destination, source);
    printf("destination string is : %s", destination);  // Output: destination string is : My World
    
    return 0;
}

Output:

destination string is : My World

C strcpy() method – Interview Questions

Q 1: What is the purpose of strcpy()?
Ans: To copy one string into another.
Q 2: What is the syntax of strcpy()?
Ans: strcpy(destination, source);
Q 3: Is strcpy() safe?
Ans: No, it does not check buffer size.
Q 4: Which header file is required for strcpy()?
Ans: .
Q 5: What happens if destination size is smaller?
Ans: It causes buffer overflow.

C strcpy() method – Objective Questions (MCQs)

Q1. What is the purpose of strcpy() in C?






Q2. Which is the correct syntax of strcpy()?






Q3. Which header file is needed for strcpy()?






Q4. What happens if the destination array is smaller than the source in strcpy()?






Q5. What will be the output?

char src[] = "Hello";
char dest[10];
strcpy(dest, src);
printf("%s", dest);






Related C strcpy() method Topics