strcmp
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <string.h> で定義
|
||
int strcmp( const char *lhs, const char *rhs ); |
||
2つのヌル終端バイト文字列を辞書的に比較します。
結果の符号は比較する文字列内の最初の異なる文字の組の値 (どちらも unsigned char として解釈されます) の差の符号です。
lhs または rhs がヌル終端バイト文字列を指すポインタでない場合、動作は未定義です。
引数
| lhs, rhs | - | 比較するヌル終端バイト文字列を指すポインタ |
戻り値
辞書順で lhs が rhs より前に現れる場合は負の値。
lhs と rhs が等しい場合はゼロ。
辞書順で lhs が rhs より後に現れる場合は正の値。
ノート
この関数は、 strcoll や strxfrm と異なり、ロケール対応ではありません。
例
Run this code
#include <string.h>
#include <stdio.h>
void demo(const char* lhs, const char* rhs)
{
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!]
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.24.4.2 The strcmp function (p: 365-366)