Libft(7) - memcmp

2021. 6. 26. 16:4542seoul/42 Cursus

Libft(7) - memcmp

 

MEMCMP(3)                BSD Library Functions Manual                MEMCMP(3)

NAME
     memcmp -- compare byte string

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <string.h>

     int
     memcmp(const void *s1, const void *s2, size_t n);

DESCRIPTION
     The memcmp() function compares byte string s1 against byte string s2.  Both strings
     are assumed to be n bytes long.

RETURN VALUES
     The memcmp() function returns zero if the two strings are identical, otherwise
     returns the difference between the first two differing bytes (treated as unsigned
     char values, so that `\200' is greater than `\0', for example).  Zero-length strings
     are always identical.  This behavior is not required by C and portable code should
     only depend on the sign of the returned value.

SEE ALSO
     bcmp(3), strcasecmp(3), strcmp(3), strcoll(3), strxfrm(3), wmemcmp(3)

STANDARDS
     The memcmp() function conforms to ISO/IEC 9899:1990 (``ISO C90'').

BSD                              June 4, 1993                              BSD
  • s1과 s2의 n 바이트를 서로 비교함.
  • 두 문자열은 n 바이트보다 크다고 가정함.

기본 함수 구조 및 매개변수

     #include <string.h>

     int
     memcmp(const void *s1, const void *s2, size_t n);
  • s1: 비교할 문자열1.
  • s2: 비교할 문자열2.
  • n: 비교할 만큼의 바이트 수.

리턴값

  • 두 문자열에서 n 번째 바이트가 서로 같다면 0 리턴.
  • 두 문자열이 다른 경우, 앞에서부터 처음으로 다른 바이트를 살펴보는데, 그 바이트를 unsigned char로 해석했을 때, 그 값이 s1이 더 크면 양수, s2이 더 크면 음수 리턴. 
  • unsigned char이므로 '\200' 이 '\0' 보다 크게 됨. 
  • 길이가 0인 문자열은 항상 같음.
  • C에서는 필요하지 않지만 이식성을 위해서 리턴값의 부호에만 의지함.

 

참고 자료

https://modoocode.com/84

 

C 언어 레퍼런스 - memcmp 함수

 

modoocode.com

 

'42seoul > 42 Cursus' 카테고리의 다른 글

Libft(9) - strlcpy  (0) 2021.06.26
Libft(8) - strlen  (0) 2021.06.26
Libft(6) - memchr  (0) 2021.06.26
Libft(5) - memmove  (0) 2021.06.26
Libft(4) - memccpy  (0) 2021.05.22