int main(void) {
char s[] = "ab";
char *s1 = "ab";
if(strcmp(s, s1))
printf("%d", 24);
else
printf("%d", 23);
return EXIT_SUCCESS;
}
Why it is giving 23 answer?
This is because strcmp
returns 0
if strings are equal. 0
is treated as false
in C.
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
returns 0 if the given strings are equal, as in this case. 0
is converted to false
, so the else
block is executed, printing 23
.
strcmp
on success returns 0
. Hence if
will fail.
Change if
statement as,
if(strcmp(s , s1) == 0)
如果两个字符串比较相等,则strcmp
返回0。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.