std::memcmp() in C++ Last Updated : 29 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Pointer to block of a memory buffer that will be compared with a second memory buffer.buf2: Pointer to the second block of memory buffer.count: Maximum numbers of bytes to compare.Return ValuesIt returns 0 when buf1 is equal to buf2.It returns an integer value less than zero when the first different byte in both memory buffers is less in buf1 than buf2.It returns an integer value greater than zero when the first different byte that is different in both memory buffers is greater in buf1 than buf2. Note: The memcmp function does not look for '\0' NULL character in the string. It will keep comparing the bytes till the specified count. Examples of memcmp()Example 1 The following C++ program illustrates the use of memcmp() function when buf1 is greater than buf2. C++ // CPP program to illustrate std::memcmp() #include <cstring> #include <iostream> using namespace std; int main() { // defining two buffers to compare char buff1[] = "Welcome to GeeksforGeeks"; char buff2[] = "Hello Geeks "; int a; // using memcmp() a = memcmp(buff1, buff2, sizeof(buff1)); // printing results if (a > 0) cout << buff1 << " is greater than " << buff2; else if (a < 0) cout << buff1 << "is less than " << buff2; else cout << buff1 << " is the same as " << buff2; return 0; } OutputWelcome to GeeksforGeeks is greater than Hello Geeks Example 2 The following C++ program illustrates the use of memcmp() function when buf1 is less than buf2. C++ // CPP program to illustrate std::memcmp() #include <cstring> #include <iostream> using namespace std; int main() { // comparing two strings directly int comp = memcmp("GEEKSFORGEEKS", "geeksforgeeks", 6); // result output if (comp == 0) { cout << "both are equal"; } else if (comp < 0) { cout << "String 1 is less than String 2"; } else { cout << "String 1 is greater than String 2"; } } OutputString 1 is less than String 2Example 3 The following C++ program illustrates the use of memcmp() function when buf1 is equal to buf2. C++ // CPP program to illustrate std::memcmp() #include <cstring> #include <iostream> using namespace std; int main() { // two buffers char buff1[] = "Welcome to GeeksforGeeks"; char buff2[] = "Welcome to GeeksforGeeks"; int a; // defining count using sizeof operator a = memcmp(buff1, buff2, sizeof(buff1)); if (a > 0) cout << buff1 << " is greater than " << buff2; else if (a < 0) cout << buff1 << "is less than " << buff2; else cout << buff1 << " is the same as " << buff2; return 0; } OutputWelcome to GeeksforGeeks is the same as Welcome to GeeksforGeeks Comment More infoAdvertise with us Next Article memmove() in C/C++ S Shivani Ghughtyal Improve Article Tags : C++ STL CPP-Library Practice Tags : CPPSTL Similar Reads std::memchr in C++ C++ offers various standard template library functions to be used. One of them is memchr() function which is used to search for the first occurrence of a character in a specified number of characters. memchr() is defined inside <cstring> header file. Syntax of memchr()const void* memchr( const 2 min read memcpy() in C 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 v 2 min read Memset in C++ C++ memset() is a function that copies a single character for a specified number of times to the given bytes of memory. It is useful for filling a number of bytes with a given value starting from a specific memory location.Example:C++#include <bits/stdc++.h> using namespace std; int main() { c 5 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 std::quoted in C++ 14 std::quoted is an I/O manipulator function that was introduced in C++ 14 as the part of <iomanip> library. Its primary purpose is to handle the quoted string in the input and output operations. In this article, we will learn about the std::quoted manipulator, how it works and how to use it in 5 min read multimap rend in C++ STL multimap::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element preceding to the first element of the multimap container. Syntax multimap_name.rend() Parameters: The function does not take any parameter. Return ValueThe function returns a rever 2 min read Like