##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# memcmp

From cppreference.com
< c‎ | string‎ | byte

Defined in header `<string.h>` |  |
int memcmp( const void* lhs, const void* rhs, size_t count ); |  |

Compares the first `count` characters of the objects pointed to by `lhs` and `rhs`. The comparison is done lexicographically.

### [edit] Parameters

lhs, rhs | - | pointers to the memory buffers to compare
count | - | number of bytes to examine

### [edit] Return value

Negative value if `lhs` is less than `rhs`.

​0​ if `lhs` is equal to `rhs`.

Positive value if `lhs` is greater than `rhs`.

### [edit] Example

Run this code

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
        char a1[] = {'a', 'b', 'c'};
        char a2[] = {'a', 'b', 'd'};

        printf("'abc' vs 'abd': %2d\n", memcmp(a1, a2, sizeof a1));
        printf("'abd' vs 'abc': %2d\n", memcmp(a2, a1, sizeof a1));
        printf("'abc' vs 'abc': %2d\n", memcmp(a1, a1, sizeof a1));

        return 0;
    }

Output:

    'abc' vs 'abd': -1
    'abd' vs 'abc':  1
    'abc' vs 'abc':  0

### [edit] See also

strcmp | compares two strings (function) [edit]
strncmp | compares a certain amount of characters of two strings (function) [edit]
C++ documentation for memcmp

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/string/byte/memcmp&oldid=70103"