C

[C] 101 메모리 비교하기 memcmp()

qkrwngus 2021. 1. 28. 18:42

Desc :

int memcmp( const void* buf1, const void* buf2, unsigned int count );

buf1, buf2 - 비교할 버퍼

count - 비교할 버퍼의 크기

 

--> 값이 같다 0

buf1 < buf2 = -1

buf1 > buf2 = 1


Source Code :

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

void main()
{
	char s1[100] = "123";
	char s2[100] = "123";

	strcpy(&s1[4], "abc");	// s1번지에서 4를 더한 번지에 문자열 복사
	strcpy(&s2[4], "efg");

	if (strcmp(s1, s2) == 0)	// 중간 널 값까지만 비교됨(3바이트)
	{
		puts("strcmp: 버퍼의 값이 일치합니다.");
	}

	if (memcmp(s1, s2, 7) == 0)	// 7바이트를 비교한다
	{
		puts("memcmp: 버퍼의 값이 일치합니다.");
	}
	else
	{
		printf("memcmp: 버퍼의 값이 일치하지 않습니다.(%d)\n", memcmp(s1, s2, 7));
	}
}

 


Result :