[英]comparing character array and character pointer using strcmp
int main(void) {
char s[] = "ab";
char *s1 = "ab";
if(strcmp(s, s1))
printf("%d", 24);
else
printf("%d", 23);
return EXIT_SUCCESS;
}
为什么给出23个答案?
这是因为如果字符串相等,则strcmp
返回0
。 0
在C中被视为false
。
strcmp(string1, string2)
if Return value < 0 then it indicates string1 is less than string2
if Return value > 0 then it indicates string2 is less than string1
if Return value = 0 then it indicates string1 is equal to string2
如果给定的字符串相等,则strcmp
返回0,在这种情况下。 0
转换为false
,因此执行else
块,打印23
。
成功的strcmp
返回0
。 因此, if
失败。
将if
语句更改为
if(strcmp(s , s1) == 0)
如果两个字符串比较相等,则strcmp
返回0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.