strcmp
來自 cppreference.com
定義於標頭檔案 <string.h> |
||
int strcmp( const char* lhs, const char* rhs ); |
||
按字典順序比較兩個以空字元結尾的位元組字串。
結果的符號是所比較字串中第一對不同字元的值(都解釋為 unsigned char)之差的符號。
如果 lhs 或 rhs 不是指向以空字元結尾的位元組字串的指標,則行為未定義。
目錄 |
[編輯] 引數
lhs, rhs | - | 指向要比較的空終止位元組字串的指標 |
[編輯] 返回值
如果 lhs 在字典序上出現在 rhs 之前,則返回負值。
如果 lhs 和 rhs 比較相等,則返回零。
如果 lhs 在字典序上出現在 rhs 之後,則返回正值。
[編輯] 注意
與 strcoll 和 strxfrm 不同,此函式不區分割槽域設定。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <string.h> void demo(const char* lhs, const char* rhs) { const int rc = strcmp(lhs, rhs); const char* rel = rc < 0 ? "precedes" : rc > 0 ? "follows" : "equals"; printf("[%s] %s [%s]\n", lhs, rel, rhs); } int main(void) { const char* string = "Hello World!"; demo(string, "Hello!"); demo(string, "Hello"); demo(string, "Hello there"); demo("Hello, everybody!" + 12, "Hello, somebody!" + 11); }
輸出
[Hello World!] precedes [Hello!] [Hello World!] follows [Hello] [Hello World!] precedes [Hello there] [body!] equals [body!]
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.24.4.2 strcmp 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.24.4.2 strcmp 函式 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.24.4.2 strcmp 函式 (p: 365-366)
- C99 標準 (ISO/IEC 9899:1999)
- 7.21.4.2 strcmp 函式 (p: 328-329)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.11.4.2 strcmp 函式
[編輯] 參見
比較兩個字串的特定數量的字元 (函式) | |
(C95) |
比較兩個寬字串 (函式) |
比較兩個緩衝區 (函式) | |
根據當前區域設定比較兩個字串 (函式) | |
C++ 文件 用於 strcmp
|