(转)strncmp, wcsncmp详解

Parameters
string1, string2
Strings to compare.
count
Number of characters to compare.
Return Values

The return value indicates the relation of the substrings of string1 and string2 as follows.

Return ValueDescription
< 0string1 substring less than string2 substring
0string1 substring identical to string2 substring
> 0string1 substring greater than string2 substring
Remarks

The strncmp function lexicographically compares, at most, the first count characters in string1 and string2 and returns a value indicating the relationship between the substrings. strncmp is case-sensitive. Unlike strcoll, strncmp is not affected by locale.

wcsncmp is the wide-character version of strncmp. The arguments and return value of wcsncmp are wide-character strings. These two functions behave identically otherwise. wcsncmp is the case-sensitive version of _wcsnicmp.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE Defined
_tcsnccmpwcsncmp
_tcsncmpwcsncmp

For more information about TCHAR.H routines, see Generic Text Mappings.

Example

/* 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";

void main( void )
{
   char tmp[20];
   int result;
   printf( "Compare strings:/n/t/t%s/n/t/t%s/n/n", string1, string2 );
   printf( "Function:/tstrncmp (first 10 characters only)/n" );
   result = strncmp( string1, string2 , 10 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "Result:/t/tString 1 is %s string 2/n/n", tmp );
   printf( "Function:/tstrnicmp _strnicmp (first 10 characters only)/n" );
   result = _strnicmp( string1, string2, 10 );
   if( result > 0 )
      strcpy( tmp, "greater than" );
   else if( result < 0 )
      strcpy( tmp, "less than" );
   else
      strcpy( tmp, "equal to" );
   printf( "Result:/t/tString 1 is %s string 2/n/n", tmp );
}

Output

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 (first 10 characters only)
Result:      String 1 is equal to string 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值