我可能会问一个愚蠢的问题,但我真的找不到谷歌的答案,我仍然是使用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 );
}
结果显示它们是相同的:
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
我想知道为什么......
答案 0 :(得分:7)
stricmp
是一个POSIX函数,而不是标准的C90函数。为避免名称冲突,Microsoft弃用了不合格的名称(stricmp
),建议使用_stricmp
。功能没有区别(stricmp
仅仅是_stricmp
的别名。)
答案 1 :(得分:3)
对于许多库函数,包括所有<string.h>
函数,下划线前缀版本是微软的想法。我不记得究竟是什么。
非强制版本非常便携。使用_stricmp()
,_strcpy()
等的代码必须以某种方式处理 - 编辑,#defined
等 - 如果代码将由其他编译器处理。