標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
ISO C |
両方 |
#include <string.h>
int strncmp(const char *string1, const char *string2, size_t count);
strncmp() 組み込み関数では、string1 で示され るストリングの最初の count 文字と string2 で示さ れるストリングが比較されるにすぎません。
関数のストリング引数には、ストリングの終わりにマークを付ける NULL 文字 (¥0) が含まれていなくてはなりません。
ストリング間の関係は、異なる左端の最初の対の文字の値の間の 差の符号で判別されます。値は、エンコード文字によって異なります。この関数はロケールに依存しません。
⁄* CELEBS45
This example demonstrates the difference between &strcmp.
and &strncmp..
*⁄
#include <stdio.h>
#include <string.h>
#define SIZE 10
int index = 3;
int main(void)
{
int result;
char buffer1[SIZE] = "abcdefg";
char buffer2[SIZE] = "abcfg";
void print_result( int, char *, char * );
result = strcmp( buffer1, buffer2 );
printf( " strcmp: compares each character¥n");
print_result( result, buffer1, buffer2 );
result = strncmp( buffer1, buffer2, index);
printf( "¥nstrncmp: compares only the first %i characters¥n", index );
print_result( result, buffer1, buffer2 );
}
void print_result( int res, char * p_buffer1, char * p_buffer2 )
{
if ( res == 0 )
printf( "first %i characters of ¥"%s¥" is identical to ¥"%s¥"¥n",
index, p_buffer1, p_buffer2);
else if ( res < 0 )
printf( "¥"%s¥" is less than ¥"%s¥"¥n", p_buffer1, p_buffer2 );
else
printf( "¥"%s¥" is greater than ¥"%s¥"¥n", p_buffer1, p_buffer2 );
}
strcmp: compares each character
"abcdefg" is less than "abcfg"
strncmp: compares only the first 3 characters
first 3 characters of "abcdefg" is identical to "abcfg"