跳轉到內容

C 程式設計/wchar.h/wcscmp

來自華夏公益教科書,自由的教科書

在 C 語言中,函式 wcscmp 包含在標頭檔案 wchar.h 中。wcscmp 與 strcmp 類似,即用於比較兩個字串。但函式 wcscmp 用於比較寬字元字串。如果要比較兩個寬字元字串,例如 s1 和 s2。然後函式 wcscmp 如果 s1 大於 s2 則返回正整數。如果字串 s2 大於 s1,則返回負整數。如果兩個寬字元字串即 s1 和 s2 相同,則函式返回 0。

#include <wchar.h>
      int wcscmp(const wchar_t *s1, const wchar_t *s2);

使用 wcsncmp 函式的示例程式

[編輯 | 編輯原始碼]
#include <stdio.h>
#include <wchar.h>
int main() {
        wchar_t string1[] = L"char";
        wchar_t string2[] = L"character";
        int difference;

        difference = wcscmp( string1, string2 );
        if ( difference == 0 )
                printf( " Both strings are same"  );
        else {
                if ( difference < 0 )
                        printf( " char is less than character\n" );
                else
                        printf(" char is greater than character\n" );
        }
        return 0;
}

程式的輸出將是“字元小於字元”,
因為差值的為負數。

  • Wcsncmp
華夏公益教科書