描述
C庫函數int memcmp(const void *str1, const void *str2, size_t n))比較第一個n內存區域的字節數str1和內存區str2。
聲明
以下是 memcmp() 函數的聲明。
int memcmp(const void *str1, const void *str2, size_t n)
參數
str1- 這是指向內存塊的指針。
str2- 這是指向內存塊的指針。
n- 這是要比較的字節數。
返回值
如果返回值 < 0,則表示 str1 小於 str2。
如果返回值 > 0,則表示 str2 小於 str1。
如果返回值 = 0,則表示 str1 等於 str2。
示例
下麵的例子展示了 memcmp() 函數的用法。
#include <stdio.h>
#include <string.h>
int main () {
char str1[15];
char str2[15];
int ret;
memcpy(str1, "abcdef", 6);
memcpy(str2, "ABCDEF", 6);
ret = memcmp(str1, str2, 5);
if(ret > 0) {
printf("str2 is less than str1");
} else if(ret < 0) {
printf("str1 is less than str2");
} else {
printf("str1 is equal to str2");
}
return(0);
}
讓我們編譯並運行上麵的程序,將產生以下結果 -
str2 is less than str1
相關用法
- C語言 memchr()用法及代碼示例
- C語言 memcpy()用法及代碼示例
- C語言 memmove()用法及代碼示例
- C語言 memset()用法及代碼示例
- C語言 moveto()用法及代碼示例
- C語言 mbtowc()用法及代碼示例
- C語言 modf()用法及代碼示例
- C語言 moverel()用法及代碼示例
- C語言 mktime()用法及代碼示例
- C語言 malloc()用法及代碼示例
- C語言 mblen()用法及代碼示例
- C語言 mbstowcs()用法及代碼示例
- C語言 宏 assert()用法及代碼示例
- C語言 vprintf()用法及代碼示例
- C語言 宏 va_start()用法及代碼示例
- C語言 setlocale()用法及代碼示例
- C語言 fread()用法及代碼示例
- C語言 sinh()用法及代碼示例
- C語言 宏 offsetof()用法及代碼示例
- C語言 feof()用法及代碼示例
注:本文由純淨天空篩選整理自 C library function - memcmp()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。