strcmp在c语言中的意思是string compare的缩写,用于比较两个字符串并根据比较结果返回整数,基本形式为strcmp(str1,str2),若【str1=str2】,则返回零,若【str1
strcmp在c语言中的意思是:
strcmp
函数是string compare(字符串比较)的缩写,用于比较两个字符串并根据比较结果返回整数。基本形式为strcmp(str1,str2)
,若str1=str2,则返回零;若str1
当s1
当s1=s2时,返回值= 0;
当s1>s2时,返回正数。
即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。如:
1."A"<"B"
2."A"<"AB"
3."Apple"<"Banana"
4."A"<"a"
5."compare"<"computer"
特别注意:strcmp(const char *s1,const char * s2)
这里面只能比较字符串,即可用于比较两个字符串常量,或比较数组和字符串常量,不能比较数字等其他形式的参数。
ANSI标准规定,返回值为正数,负数,0 。而确切数值是依赖不同的C实现的。
当两个字符串不相等时,C标准没有规定返回值会是1 或 -1,只规定了正数和负数。
有些会把两个字符的ASCII码之差作为比较结果由函数值返回。但无论如何不能以此条依据作为程序中的流程逻辑。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
相关学习推荐:C视频教程
The above is the detailed content of What does strcmp mean in c language?. For more information, please follow other related articles on the PHP Chinese website!