フォーマット
#include <string.h> int memcmp(const void *buf1, const void *buf2, size_t count);
言語レベル: ANSI
スレッド・セーフ: はい。
説明
memcmp() 関数は、buf1 と buf2 の先頭 count バイトを比較します。
戻り値
memcmp() 関数は、バッファー間の関係を示す次のような値を戻します。
値 | 意味 |
0 より小さい値 | buf1 は buf2 より小さい |
0 | buf1 は buf2 と等しい |
0 より大きい値 | buf1 は buf2 より大きい |
memcmp() の使用例
この例では、main() に渡される 1 番目と 2 番目の引数を比較して、どちらが大きいかを判別します (どちらかが大きい場合)。
#include <stdio.h> #include <string.h> int main(int argc, char ** argv) { int len; int result; if ( argc != 3 ) { printf( "Usage: %s string1 string2¥n", argv[0] ); } else { /* Determine the length to be used for comparison */ if (strlen( argv[1] ) < strlen( argv[2] )) len = strlen( argv[1] ); else len = strlen( argv[2] ); result = memcmp( argv[1], argv[2], len ); printf( "When the first %i characters are compared,¥n", len ); if ( result == 0 ) printf( "¥"%s¥" is identical to ¥"%s¥"¥n", argv[1], argv[2] ); else if ( result < 0 ) printf( "¥"%s¥" is less than ¥"%s¥"¥n", argv[1], argv[2] ); else printf( "¥"%s¥" is greater than ¥"%s¥"¥n", argv[1], argv[2] ); } } /**************** If the program is passed the arguments ************** ***************** firststring and secondstring, ************ ***************** output should be: ************ When the first 11 characters are compared, "firststring" is less than "secondstring" **********************************************************************/
関連情報