Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

memcmp vs. strncmp

756 views
Skip to first unread message

Ken Bateman

unread,
May 1, 1992, 2:09:09 PM5/1/92
to
What's the difference between memcmp() and strncmp()? They take the exact
same arguments. They appear to behave identically.

Here's the pertinent info from man string(3):

strcmp() compares its arguments and returns an integer
greater than, equal to, or less than 0, according as s1 is
lexicographically greater than, equal to, or less than s2.
strncmp() makes the same comparison but compares at most n
characters.

Here's the pertinent info from man memory(3):

memcmp() compares its arguments, looking at the first n
characters only, and returns an integer less than, equal to,
or greater than 0, according as s1 is lexicographically less
than, equal to, or greater than s2.

Maybe I'm missing something obvious.

------------------------------------------------------
Ken Bateman bat...@msr.ornl.gov
"We are a hedge. Please move along" -anonymous ninja

Ken Bateman

unread,
May 1, 1992, 3:09:04 PM5/1/92
to
I've been informed of the solution to this.
Here's how one response went:

No, _you_ didn't miss anything obvious; your man pages did. :-) The key is
that `at most' in the strncmp() text: str* functions take (char)0 (ie ASCII
NUL) to be an end-of-string indicator and stop when they encounter it. The
mem* functions treat NUL as any other character.

so
"I am a string \000 with a hidden tail"
and
"I am a string \000 equiped with a sneaky part"

will compare equal with strncmp, but not with memcmp.

Ian Lepore

unread,
May 1, 1992, 4:15:31 PM5/1/92
to

> difference between memcmp() and strncmp()

strncmp() will stop comparing characters when it reaches a nullterm
character in either string or when the limit/count is reached. memcmp()
is blind to nullterm characters.

At first glance, this seems to be the same, but given two strings:

"abc\0efg\0"
"abc\0fgh\0"

Then strncmp(,,6) will say they're equal, memcmp(,,6) will not.

David F. Skoll

unread,
May 1, 1992, 4:49:17 PM5/1/92
to
In <1992May1....@ornl.gov> bat...@max.epm.ornl.gov (Ken
Bateman) writes:

>What's the difference between memcmp() and strncmp()? They take the exact
>same arguments. They appear to behave identically.

strncmp() terminates after n characters OR after hitting a '\0' character,
whichever comes first.

memcmp() does not terminate if it encounters a '\0' character.

--
David F. Skoll

Tim Purves

unread,
May 3, 1992, 1:34:23 PM5/3/92
to
In article <1992May1....@ornl.gov> bat...@msr.ornl.gov writes:
>What's the difference between memcmp() and strncmp()? They take the exact
>same arguments. They appear to behave identically.
>
>Here's the pertinent info from man string(3):
B

>
> strcmp() compares its arguments and returns an integer
> greater than, equal to, or less than 0, according as s1 is
> lexicographically greater than, equal to, or less than s2.
> strncmp() makes the same comparison but compares at most n
> characters.
>
>Here's the pertinent info from man memory(3):
>
> memcmp() compares its arguments, looking at the first n
> characters only, and returns an integer less than, equal to,
> or greater than 0, according as s1 is lexicographically less
> than, equal to, or greater than s2.
>

strncmp, stops at the first '\0'

0 new messages