For*_*Cpp 11 c++ case-sensitive case-insensitive string-comparison
我可能会问一个愚蠢的问题,但我真的找不到谷歌的答案加上我仍然是使用MSVS的初学者.
我最近需要使用函数来比较两个字符串.我不明白的是stricmp和_stricmp的区别.它们都可用于比较字符串并返回相同的结果.我去检查一下:
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";
void main( void )
{
char tmp[20];
int result;
/* Case sensitive */
printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
result = stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\tstricmp: String 1 is %s string 2\n", tmp );
/* Case insensitive */
result = _stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\t_stricmp: String 1 is %s string 2\n", tmp );
}
Run Code Online (Sandbox Code Playgroud)
结果显示它们是相同的:
Compare strings:
The quick brown dog jumps over the lazy fox
The QUICK brown dog jumps over the lazy fox
stricmp: String 1 is equal to string 2
_stricmp: String 1 is equal to string 2
Run Code Online (Sandbox Code Playgroud)
我想知道为什么......
Sta*_*lot 10
stricmp是POSIX函数,而不是标准的C90函数.为了避免名称冲突,Microsoft弃用了不合格的名称(stricmp)并建议使用_stricmp.功能没有区别(stricmp仅仅是别名_stricmp.)
对于许多库函数,包括所有<string.h>函数,下划线前缀版本是/是微软的想法。具体是什么我不记得了。
非下划线版本是高度便携的。使用_stricmp()、_strcpy()等的代码必须以某种方式处理——编辑#defined、 等——如果代码将由另一个编译器处理。