# Help in memcmp (it may be a bug)

**URL:** <https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886>\
**Category:** Programming\
**Created:** [March 21, 2012, 4:42am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886 "2012-03-21T04:42:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rink](https://community.unix.com/letter_avatar/rink/32/5_5575768a8748004e209b776fc1b2916d.png) [@rink](https://community.unix.com/u/rink)\
**Post date:** [March 21, 2012, 4:42am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/1 "2012-03-21T04:42:44Z")

</div>

Hello friends today i have created a program that is working fine . but when string becomes equal it does not work .

```nohighlight
/* String.h header series
	
	Comparison function

memcmp = return 0 if string equal else return less or greater than value by comparing pointer first and pointer second

*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
/*memcmp operation*/
int i;
char str[20];
char str2[20];
printf("Enter first string: ");
fgets(str,20,stdin);
printf("Enter second string: ");
fgets(str2,20,stdin);
fflush(stdin);
i = memcmp(str,str2,20);
if(i>0){
printf("%s greater than %s\n",str,str2);
}
else if(i<0)	{
	printf("%s greater than %s\n",str2,str);
}
else { printf("Both are eqaul\n"); }
return 0;  
}

```

output:

```nohighlight
[root@apache ~]# ./mem
Enter first string: hello
Enter second string: hello
hello greater than hello

```

memcmp is not working if string equal and it does not return 0 on equality of string , is this a bug ?

any help

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [March 21, 2012, 4:52am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/2 "2012-03-21T04:52:15Z")

</div>

for the same input "hello"

can you try with the below code

```nohighlight
 
i = memcmp(str,str2,5);

```

---

<div class="post-metadata">

**Author:** ![frank\_rizzo](https://community.unix.com/letter_avatar/frank_rizzo/32/5_5575768a8748004e209b776fc1b2916d.png) [@frank\_rizzo](https://community.unix.com/u/frank_rizzo)\
**Post date:** [March 21, 2012, 7:15am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/3 "2012-03-21T07:15:51Z")

</div>

Consider using [man strcmp ("3")](https://www.unix.com/man-pages.php?query=strcmp&section=0&os=%223%22)

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [March 21, 2012, 7:59am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/4 "2012-03-21T07:59:41Z")

</div>

hello is 5 characters long. Not 20.

strcmp, strncmp and other similar functions are used to do string compares. A C string is terminated with a nul (ascii 0) character. This allows strcmp to determine the string's length and stop comparing. memcmp compares objects in memory. It does not check the length because it is meant to compare memory objects which often have ascii 0 characters.

You provide the length to memcmp. 20 is not the length of the word hello

---

<div class="post-metadata">

**Author:** ![pflynn](https://community.unix.com/letter_avatar/pflynn/32/5_5575768a8748004e209b776fc1b2916d.png) [@pflynn](https://community.unix.com/u/pflynn)\
**Post date:** [March 21, 2012, 9:01am UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/5 "2012-03-21T09:01:37Z")

</div>

To the program work the way you expect, you need to initialize the str and str2 memory areas with the same values (say, with all zeroes, for example):

```php
(...)
char str[20];
char str2[20];

memset(str, 0, sizeof(str)); 
memset(str2, 0, sizeof(str2));
(...)

```

If you don�t initialize str and str2 with the same values, these areas will contain random values, so the memcmp comparison will not match, even if they both have some values in common (both begin with 'h','e','l','l','o', but you don�t know what each have after the 5 first characters, cause it is uninitialized memory).

---

<div class="post-metadata">

**Author:** ![rink](https://community.unix.com/letter_avatar/rink/32/5_5575768a8748004e209b776fc1b2916d.png) [@rink](https://community.unix.com/u/rink)\
**Post date:** [March 21, 2012, 1:28pm UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/6 "2012-03-21T13:28:17Z")

</div>

Thanks to pflynn now it is working

i have used memset as u say it great idea first get string and fill it with 0 then compare it .

thanks pflynn

---

<div class="post-metadata">

**Author:** ![pflynn](https://community.unix.com/letter_avatar/pflynn/32/5_5575768a8748004e209b776fc1b2916d.png) [@pflynn](https://community.unix.com/u/pflynn)\
**Post date:** [March 21, 2012, 1:55pm UTC](https://community.unix.com/t/help-in-memcmp-it-may-be-a-bug/306886/7 "2012-03-21T13:55:42Z")

</div>

Yes Rink, but be aware that for string comparisons, strcmp - and its safe version, strncmp - are better suited, since they are specifically designed for working with strings.
