C Reference String Operation: memcmp()

The function memcmp() compares n bytes of two regions of memory, treating each byte as an unsigned character.

Usage:


int memcmp(const void *target, const void *source, size_t n);

If the two buffers are equal then zero is returned.

If the target buffer is less than the source buffer then a value that is less then zero is returned.

If the target buffer is greater than the source buffer then a value that is greater then zero is returned.

memcmp() source code example:


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

	main()
	{
		char line[100];
		char line2[100];

		strcpy(line, "aaa");
		strcpy(line2,"aa");

		if (memcmp( line, line2, 2) == 0)
			printf ("equal\n");

		if (memcmp( line, line2, 2) < 0)
			printf("less then equal\n");

		if (memcmp( line, line2, 2) > 0)
			printf("greater then equal\n");
	}

Output of the example:


	equal

Note: the result is equal because the count was set to two.

This entry was posted in C Reference string.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: