C 語言標準函數庫分類導覽 - string.h memcmp()

string.h 的函數 memcmp() 類似 strncmp() ,比較兩個記憶體區段前 n 個字元是否相等。



以下程式印出比較字串 s 及 t ,函數 memcmp() 的回傳值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char s[] = "Time flies.";
    char t[] = "Time is money.";
     
    printf("%d\n", memcmp(s, t, 4));
    printf("%d\n", memcmp(s, t, 10));
    printf("%d\n", memcmp(t, s, 10));
     
    return 0;   
}
 
/* 《程式語言教學誌》的範例程式
    檔名:cmemcmp.c
    功能:示範 string.h 中函數 memcmp() 的使用
    作者:張凱慶
    時間:西元2010年6月 */


編譯後執行,結果如下



您可以繼續參考


沒有留言: