0

Why does strcmp() working like that?

char *str1 = "c", *str2 = "C";
printf("Ready(c to C): %d\n", strcmp(str1, str2)); // return 32, because 99 - 67 = 32
printf("Ready(c to C): %d\n", strcmp("c", "C")); // return 1, because 99 > 67 = 1
  • 1
    `strcmp` returns a non-0 value when the strings are unequal, either `> 0` or `< 0`. Beyond that, its behaviour is not defined, and the compiler can take whatever shortcuts it wants to comply. – Weather Vane May 24 '20 at 21:11
  • 2
    The chances are that the second one (with two string literals) is actually computed at compile time and the other is not. Both results are valid — there's no requirement that `strcmp()` always produce the same answer for the same arguments as long as the answer is correct. – Jonathan Leffler May 24 '20 at 21:11

0 Answers0