
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <float.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdarg.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <time.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C library function - memcmp()
Description
The C library function int memcmp(const void *str1, const void *str2, size_t n)) compares the first n bytes of memory area str1 and memory area str2.
Declaration
Following is the declaration for memcmp() function.
int memcmp(const void *str1, const void *str2, size_t n)
Parameters
str1 − This is the pointer to a block of memory.
str2 − This is the pointer to a block of memory.
n − This is the number of bytes to be compared.
Return Value
if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.
Example
The following example shows the usage of memcmp() function.
#include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; memcpy(str1, "abcdef", 6); memcpy(str2, "ABCDEF", 6); ret = memcmp(str1, str2, 5); if(ret > 0) { printf("str2 is less than str1"); } else if(ret < 0) { printf("str1 is less than str2"); } else { printf("str1 is equal to str2"); } return(0); }
Let us compile and run the above program that will produce the following result −
str2 is less than str1
string_h.htm
Advertisements