Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcmp() return different values for same string comparisons [duplicate]

char s1[] = "0";
char s2[] = "9";
printf("%d\n", strcmp(s1, s2));   // Prints -9
printf("%d\n", strcmp("0", "9")); // Prints -1

Why do strcmp returns different values when it receives the same parameters ?

Those values are still legal since strcmp's man page says that the return value of strcmp can be less, greater or equal than 0, but I don't understand why they are different in this example.

like image 896
Bilow Avatar asked Oct 12 '15 22:10

Bilow


People also ask

What are the return values of strcmp function for comparing strings?

The return value from strcmp is 0 if the two strings are equal, less than 0 if str1 compares less than str2 , and greater than 0 if str1 compares greater than str2 .

What does the strcmp () function do?

The strcmp() built-in function compares the string pointed to by string1 to the string pointed to by string2 The string arguments to the function must contain a NULL character ( \0 ) marking the end of the string.

Does strcmp return true or false?

The strcmp function takes two input arguments (two strings) and returns either true or false, just like any boolean expression. Strcmp will only return true if every character of both strings is the same and they are the same length. In all other cases, strcmp will return false.

Does strcmp work on strings?

strcmp() compares the two strings lexicographically means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.


2 Answers

I assume you are using GCC when compiling this, I tried it on 4.8.4. The trick here is that GCC understands the semantics of certain standard library functions (strcmp being one of them). In your case, the compiler will completely eliminate the second strcmp call, because it knows that the result of strcmpgiven string constants "0" and "9" will be negative, and a standard compatible value (-1) will be used instead of doing the call. It cannot do the same with the first call, because s1 and s2 might have been changed in memory (imagine an interrupt, or multiple threads, etc.).

You can do an experiment to validate this. Add the const qualifier to the arrays to let GCC know that they cannot be changed:

const char s1[] = "0";
const char s2[] = "9";
printf("%d\n", strcmp(s1, s2));   // Now this will print -1 as well
printf("%d\n", strcmp("0", "9")); // Prints -1

You can also look at the assembler output form the compiler (use the -S flag).

The best way to check however is to use -fno-builtin, which disables this optimization. With this option, your original code will print -9 in both cases

like image 55
Geza Lore Avatar answered Oct 22 '22 07:10

Geza Lore


The difference is due to the implementation of strcmp. As long as it conforms to the (<0, 0, >0), it shouldn't matter to the developer. You cannot rely on anything else. For all you know, the source code could be determining it should be negative, and randomly generating a negative number to throw you off.

like image 28
ergonaut Avatar answered Oct 22 '22 05:10

ergonaut