std::memcmp() in C++
It compares the first count characters of the arrays pointed to by buf1 and buf2.
Syntax:
int memcmp(const void *buf1, const void *buf2, size_t count); Return Value: it returns an integer. Parameters: buf1 : Pointer to block of memory. buf2 : Pointer to block of memory. count : Maximum numbers of bytes to compare. Return Value is interpreted as : Value Meaning Less than zero buf1 is less than buf2. Zero buf1 is equal to buf2. Greater than zero buf1 is greater than buf2.
Example 1. When count Greater than zero( > 0)
// CPP program to illustrate std::memcmp() #include <iostream> #include <cstring> int main() { char buff1[] = "Welcome to GeeksforGeeks"; char buff2[] = "Hello Geeks "; int a; a = std::memcmp(buff1, buff2, sizeof(buff1)); if (a > 0) std::cout << buff1 << " is greater than " << buff2; else if (a < 0) std::cout << buff1 << "is less than " << buff2; else std::cout << buff1 << " is the same as " << buff2; return 0; } |
chevron_right
filter_none
Output:
Welcome to GeeksforGeeks is greater than Hello Geeks
Example 2. When count less than zero( < 0)
// CPP program to illustrate std::memcmp() #include <cstring> #include <iostream> int main() { int comp = memcmp("GEEKSFORGEEKS", "geeksforgeeks", 6); if (comp == 0) { std::cout << "both are equal"; } if (comp < 0) { std::cout << "String 1 is less than String 2"; } else { std::cout << "String 1 is greater than String 2"; } } |
chevron_right
filter_none
Output:
String 1 is less than String 2
Example 3 : When count equal to zero ( = 0)
// CPP program to illustrate std::memcmp() #include <iostream> #include <cstring> int main() { char buff1[] = "Welcome to GeeksforGeeks"; char buff2[] = "Welcome to GeeksforGeeks"; int a; a = std::memcmp(buff1, buff2, sizeof(buff1)); if (a > 0) std::cout << buff1 << " is greater than " << buff2; else if (a < 0) std::cout << buff1 << "is less than " << buff2; else std::cout << buff1 << " is the same as " << buff2; return 0; } |
chevron_right
filter_none
Output:
Welcome to GeeksforGeeks is the same as Welcome to GeeksforGeeks
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in C++
- How can we use Comma operator in place of curly braces?
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Remove first adjacent pairs of similar characters until possible
- Find a number containing N - 1 set bits at even positions from the right
- Sort an array according to absolute difference with given value using Functors
- Rearrange characters in a string such that no two adjacent are same using hashing
- Remove duplicate elements in an Array using STL in C++



