比较高达两个字符串指定数量的字符。
重要
_mbsncmp 和 _mbsncmp_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数。
语法
int strncmp(
const char *string1,
const char *string2,
size_t count
);
int wcsncmp(
const wchar_t *string1,
const wchar_t *string2,
size_t count
);
int _mbsncmp(
const unsigned char *string1,
const unsigned char *string2,
size_t count
);
int _mbsncmp_l(
const unsigned char *string1,
const unsigned char *string2,
size_t count,
_locale_t locale
);int _mbsnbcmp(
const unsigned char *string1,
const unsigned char *string2,
size_t count
);
参数
%>
要比较的字符串。
count
要比较的字符数。
locale
要使用的区域设置。
返回值
返回值指示 string1 和 string2 的子字符串之间关系,如下所示。
| 返回值 | 说明 |
|---|---|
| < 0 | string1 的子字符串小于 string2 的子字符串 |
| 0 | string1 的子字符串等于 string2 的子字符串 |
| > 0 | string1 的子字符串大于 string2 的子字符串 |
参数验证错误时,_mbsncmp 和 _mbsncmp_l 返回 _NLSCMPERROR,该返回值是在 <string.h> 和 <mbstring.h> 中定义的。
备注
strncmp 函数对 count 和 string1 中最多前 string2 个字符执行序号比较,并返回一个指示子字符串之间关系的值。 strncmp 是 _strnicmp 的区分大小写版本。 wcsncmp 和 _mbsncmp 是 _wcsnicmp 和 _mbsnicmp 的一个区分大小写的版本。
wcsncmp 和 _mbsncmp 分别是 strncmp 的宽字符及多字节字符版本。 wcsncmp 的自变量是宽字符字符串。 _mbsncmp 的自变量是多字节字符字符串。 _mbsncmp 根据多字节代码页识别多字节字符序列,并在发生错误时返回 _NLSCMPERROR。
此外,_mbsncmp 和 _mbsncmp_l 验证参数。 如果 string1 或 string2 是空指针,且 count 不等于 0,则将调用无效的参数处理程序,如参数验证中所述。 如果允许继续执行,则 _mbsncmp 和 _mbsncmp_l 返回 _NLSCMPERROR,并将 errno 设置为 EINVAL。 strncmp 和 wcsncmp 不会验证其参数。 否则这些函数具有相同行为。
_mbsncmp 和 _mbsncmp_l 的比较行为受到区域设置的 LC_CTYPE 类别设置的设置影响。 这会控制对多字节字符的前导和尾随字节的检测。 有关详细信息,请参阅 setlocale。 _mbsncmp 函数对与区域设置相关的行为使用当前区域设置。 _mbsncmp_l 函数完全相同,只不过它转而使用 locale 参数。 有关详细信息,请参阅 Locale。 如果区域设置是单字节的,则这些函数的行为等同于 strncmp。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
一般文本例程映射
TCHAR.H 例程 |
_UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
|---|---|---|---|
_tcsnccmp |
strncmp |
_mbsncmp |
wcsncmp |
_tcsncmp |
strncmp |
_mbsnbcmp |
wcsncmp |
_tccmp |
映射到宏或内联函数 | _mbsncmp |
映射到宏或内联函数 |
要求
| 例程 | 必需的标头 |
|---|---|
strncmp |
<string.h> |
wcsncmp |
<string.h> 或 <wchar.h> |
| %> | <mbstring.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_strncmp.c
#include <string.h>
#include <stdio.h>
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown fox jumps over the lazy dog";
int main( void )
{
char tmp[20];
int result;
printf( "Compare strings:\n %s\n %s\n\n",
string1, string2 );
printf( "Function: strncmp (first 10 characters only)\n" );
result = strncmp( string1, string2 , 10 );
if( result > 0 )
strcpy_s( tmp, sizeof(tmp), "greater than" );
else if( result < 0 )
strcpy_s( tmp, sizeof(tmp), "less than" );
else
strcpy_s( tmp, sizeof(tmp), "equal to" );
printf( "Result: String 1 is %s string 2\n\n", tmp );
printf( "Function: strnicmp _strnicmp (first 10 characters only)\n" );
result = _strnicmp( string1, string2, 10 );
if( result > 0 )
strcpy_s( tmp, sizeof(tmp), "greater than" );
else if( result < 0 )
strcpy_s( tmp, sizeof(tmp), "less than" );
else
strcpy_s( tmp, sizeof(tmp), "equal to" );
printf( "Result: String 1 is %s string 2\n", tmp );
}
Compare strings:
The quick brown dog jumps over the lazy fox
The QUICK brown fox jumps over the lazy dog
Function: strncmp (first 10 characters only)
Result: String 1 is greater than string 2
Function: strnicmp _strnicmp (first 10 characters only)
Result: String 1 is equal to string 2
另请参阅
字符串操作
区域设置
多字节字符序列的解释
%>
%>
.- .
strcoll 函数