memcpy() in C Last Updated : 01 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The memcpy() function in C is defined in the <string.h> header is a part of the standard library in C. The memcpy() function is used to copy a block of memory from one location to another. Example: C #include <stdio.h> #include <string.h> // For memcpy int main() { // Initialize a variable int a = 20; int b = 10; printf("Value of b before calling memcpy: %d\n", b); // Use memcpy to copy the value of 'a' into 'b' memcpy(&b, &a, sizeof(int)); printf("Value of b after calling memcpy: %d\n", b); return 0; } OutputValue of b before calling memcpy: 10 Value of b after calling memcpy: 20 memcpy() FunctionThe memcpy function in C copies the specified number of bytes from one memory location to another memory location regardless of the type of data stored. Where both source and destination are raw memory addresses. The memcpy() function is optimized for copying memory chunks in an efficient manner and it copies memory in a byte-by-byte format. Syntax C++ memcpy(*to, *from, numBytes); Parametersto: A pointer to the memory location where the copied data will be stored.from: A pointer to the memory location from where the data is to be copied.numBytes: The number of bytes to be copied.Return ValueThis function returns a pointer to the memory location where data is copied.Copying String using memcpy() in C C // C program to demonstrate working of memcpy #include <stdio.h> #include <string.h> int main() { char str1[] = "Geeks"; char str2[6] = ""; // Copies contents of str2 to str1 memcpy(str2, str1, sizeof(str1)); printf("str2 after memcpy:"); printf("%s",str2); return 0; } Outputstr2 after memcpy:GeeksImportant Points about memcpy() in Cmemcpy() function copies the memory in a byte-by-byte format without any checks or transformations, meaning it does not handle type conversions or alignment issues, check for overflow or \0.memcpy() leads to undefined behaviour when source and destination addresses overlap as it does not handles overlapping memory regions.The memcpy() function simply copies bytes without initialising any memory. The memcpy() function makes a shallow copy as it only copies the raw bytes of the memory from one location to another. It does not perform a deep copy or handle objects at a higher level. memcpy() only copies the pointer values (i.e., the addresses they hold), not the actual objects or data those pointers reference.Note: memmove() is another library function that handles overlapping well. Comment More infoAdvertise with us Next Article std::memcmp() in C++ K kartik Follow Improve Article Tags : C++ CPP-Library Practice Tags : CPP Similar Reads std::memcmp() in C++ memcmp() function compares the first count bytes ( given number of characters ) of the memory pointed to by buf1 and buf2. memcmp() is a Standard Library function defined in <string.h> header file in C++. Syntaxint memcmp(const void *buf1, const void *buf2, size_t count);Parametersbuf1: Pointe 3 min read strcpy in C++ strcpy() is a standard library function in C++ and is used to copy one string to another. In C++ it is present in the <string.h> and <cstring> header files. Syntax: char* strcpy(char* dest, const char* src);Parameters: This method accepts the following parameters: dest: Pointer to the de 2 min read wmemcpy() function in C/C++ The wmemcpy() function is specified in header file cwchar.h and copies a specified number of character from one string to the other. This function doesn't check for any terminating null wide character in the first string called source it always copies exactly n characters to the second string called 2 min read memmove() in C/C++ memmove() is used to copy a block of memory from a location to another. It is declared in string.h // Copies "numBytes" bytes from address "from" to address "to" void * memmove(void *to, const void *from, size_t numBytes); Below is a sample C program to show the working of memmove(). C /* A C progra 2 min read snprintf() in C In C, snprintf() function is a standard library function that is used to print the specified string till a specified length in the specified format. It is defined in the <stdio.h> header file.In this article, we will learn about snprintf() function in C and how to use it in our program.SyntaxC 3 min read C++ printf() Function printf() function is originally declared under the <cstdio>header file. It prints the formatted string to the standard output stdout. Syntax: int printf(const char*word, .......) Parameters: word: represents the string that needs to be printed on the standard output stdout,....... : represents 3 min read Like