memcmp

memcmp

memcmp是比較記憶體區域buf1和buf2的前count個位元組。該函式是按位元組比較的。

基本介紹

  • 外文名:memcmp
  • 功能:比較buf1和buf2的前count個位元組
  • 所需頭檔案:#include <string.h>
  • 返回值:當buf1<buf2時,返回值<0
函式原型,功能,所需頭檔案,返回值,說明,示例程式,參考文檔,

函式原型

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

功能

比較記憶體區域buf1和buf2的前count個位元組。

所需頭檔案

#include <string.h>或#include<memory.h>

返回值

當buf1<buf2時,返回值小於0
當buf1==buf2時,返回值=0
當buf1>buf2時,返回值大於0

說明

該函式是按位元組比較的。
例如:
s1,s2為字元串時候memcmp(s1,s2,1)就是比較s1和s2的第一個位元組的ascII碼值;
memcmp(s1,s2,n)就是比較s1和s2的前n個位元組的ascII碼值;
如:char *s1="abc";
char *s2="acd";
int r=memcmp(s1,s2,3);
就是比較s1和s2的前3個位元組,第一個位元組相等,第二個位元組比較中大小已經確定,不必繼續比較第三位元組了。所以r=-1.

示例程式

#include<string.h>#include<stdio.h>int main(){char *s1 = "Hello,Programmers!";char *s2 = "Hello,Programmers!";int r;r = memcmp(s1,s2,strlen(s1));if(!r)    printf("s1 and s2 are identical\n");/*s1等於s2*/elseif(r<0)    printf("s1 is less than s2\n");/*s1小於s2*/else    printf("s1 is greater than s2\n");/*s1大於s2*/return 0;}輸出結果:s1 and s2 are identical請按任意鍵繼續...

參考文檔

參考C99文檔:
7.21.4.1 The memcmp function
Synopsis
1
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);

Description
The memcmp function compares the first n characters of the object pointed to by s1 to
the first n characters of the object pointed to by s2.
Returns
The memcmp function returns an integer greater than, equal to, or less than zero,
accordingly as the object pointed to by s1 is greater than, equal to, or less than the object
pointed to by s2.

相關詞條

熱門詞條

聯絡我們