名稱空間
變體
操作

wcscmp

來自 cppreference.com
< c‎ | string‎ | wide
在標頭檔案 <wchar.h> 中定義
int wcscmp( const wchar_t* lhs, const wchar_t* rhs );
(自 C95 起)

按字典序比較兩個空終止寬字串。

結果的符號是所比較字串中第一對不同寬字元值的差的符號。

lhsrhs 不是指向空終止寬字串的指標,則行為未定義。

目錄

[編輯] 引數

lhs, rhs - 指向要比較的以空字元結尾的寬字串的指標

[編輯] 返回值

如果 lhs 在字典序上出現在 rhs 之前,則返回負值。

lhsrhs 比較相等,則為零。

如果 lhs 在字典序上出現在 rhs 之後,則返回正值。

[編輯] 注意

此函式與 wcscoll 不同,它不對本地環境敏感,且當一起使用來自不同 Unicode 塊的字元時,或當代碼單元的順序不匹配任何排序規則時,其順序可能沒有意義。

[編輯] 示例

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
 
void demo(const wchar_t* lhs, const wchar_t* rhs)
{
    int rc = wcscmp(lhs, rhs);
    const char *rel = rc < 0 ? "precedes" : rc > 0 ? "follows" : "equals";
 
    setlocale(LC_ALL, "en_US.utf8");
    printf("[%ls] %s [%ls]\n", lhs, rel, rhs);
}
 
int main(void)
{
    const wchar_t* string = L"どうもありがとうございます";
    demo(string, L"どうも");
    demo(string, L"助かった");
    demo(string + 9, L"ありがとうございます" + 6);
}

可能的輸出

[どうもありがとうございます] follows [どうも]
[どうもありがとうございます] precedes [助かった]
[ざいます] equals [ざいます]

[編輯] 引用

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.29.4.4.1 The wcscmp function (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.29.4.4.1 The wcscmp function (p: TBD)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.4.4.1 The wcscmp function (p: 433)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.4.4.1 The wcscmp function (p: 379)

[編輯] 參閱

比較兩個寬字串中的一定數量的字元
(函式) [編輯]
比較兩個陣列中一定數量的寬字元
(函式) [編輯]
比較兩個字串
(函式) [編輯]
根據當前語言環境比較兩個寬字串
(函式) [編輯]