c memcpy
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, memcpy!";
char destination[20];
memcpy(destination, source, strlen(source) + 1);
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
Include Libraries: Include necessary libraries (
stdio.h
andstring.h
).Main Function: Define the
main()
function.Variable Declaration: Declare two character arrays
source
anddestination
.source
contains the string "Hello, memcpy!" anddestination
is an empty array of size 20.Memcpy Function: Use
memcpy()
to copy the content ofsource
todestination
. The parameters are:destination
: The memory location where the content will be copied.source
: The memory location of the content to be copied.strlen(source) + 1
: The number of bytes to copy, calculated usingstrlen(source)
(length of the string) plus 1 (to include the null terminator).Print Statements: Print the content of
source
anddestination
usingprintf()
.Return: End the
main()
function with areturn
statement, indicating successful execution.