C memcmp() Function

Last Updated:

C memcmp()

The memcmp() function compares the first n bytes of memory area str1 and memory area str2.

Example

C Compiler
#include <stdio.h> #include <string.h> int main() { char str1[] = "ABCD"; char str2[] = "abcd"; int res = memcmp(str1, str2, 3); if(res == 0) printf("str1 is equal to str2"); else if(res > 0) printf("str1 is greater than str1"); else printf("str1 is less than str2"); return 0; }

Syntax

int memcmp(const void *str1, const void *str2, size_t n)

Parameter Values

ValueTypeExplanation
str1 Required A block of memory.
str2 Required A block of memory.
n Required Number of bytes to be compared.

Return Value

ValueExplanation
+ive If str1 is greater than str2
0 If str1 is equal to str2
-ive If str1 is less than str2

Share this Page

Meet the Author