0

With this simple program, the first printf gives back 50, and the second gives back 1. At first we thought it might be caching or optimisation, but no matter what, this is what happens.

#include <stdio.h>
#include <string.h>

int compare (void const * a, void const * b) {
    return strcmp((char const *) a, (char const *) b);
}

int main(void) {
    printf("%i\n", compare((void const *) "Key2", (void const *) "Key")); // 50
    printf("%i\n", strcmp("Key2", "Key")); // 1
    return 0;
}

In comparison, https://repl.it/CZ5h/2 shows that both give back 1. But my GCC is 5.3.0.

So what is the reason for the results being different?

Also noticed that clang compiles a program that gives deterministic results.

CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
  • I certainly didn't expect to find that dup. – 2501 Jun 08 '16 at 06:53
  • @2501 Pretty sure it isn't a duplicate. I can reproduce OP's problem on 5.3.1 (on my machine) but not 4.8 or 6.1. Also, using `const char s1[]` won't fix the problem, it's not even equivalent to what OP is doing. – uh oh somebody needs a pupper Jun 08 '16 at 06:57
  • 1
    It's not necessarily an exact duplicate, but it is related. I noticed that the other question's answer mentioned `-fno-builtin`, and applying this results in both `printf` giving back 50. – CMCDragonkai Jun 08 '16 at 06:59
  • @sleeptightpupper It is an exact dup. The first answer explains the reason for this are intrinsic functions, and the second explains the return values. – 2501 Jun 08 '16 at 06:59
  • [Here's an online demo](http://melpon.org/wandbox/permlink/4Px51XZffSVdjY7j) for GCC 5.3.0 demonstrating the problem. – uh oh somebody needs a pupper Jun 08 '16 at 06:59
  • @sleeptightpupper The solution in the dup solves this problem. – 2501 Jun 08 '16 at 07:03
  • @2501 [No it doesn't.](http://melpon.org/wandbox/permlink/6VqPbHtoy7WGf1s6) – uh oh somebody needs a pupper Jun 08 '16 at 07:07
  • @sleeptightpupper As has been said above and in the dup, `-fno-builtin` solves this. – 2501 Jun 08 '16 at 07:10
  • @2501 [Here's another link with `-fno-builtin`](http://melpon.org/wandbox/permlink/ZN8tzmpAoCvwK7Ed), now it prints `50 50`. And [with OP's original code](http://melpon.org/wandbox/permlink/AXcmZKQIm6Dl62rM). – uh oh somebody needs a pupper Jun 08 '16 at 07:11
  • 2
    This is all that the POSIX standard gurantees: `Upon completion, strcmp() shall return an integer greater than, equal to, or less than 0, if the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2, respectively.` Source: http://pubs.opengroup.org/onlinepubs/009695399/functions/strcmp.html – babon Jun 08 '16 at 07:18
  • 1
    There is a discussion about this question on Meta: http://meta.stackoverflow.com/questions/325726/how-can-i-get-this-question-thats-been-dup-hammered-re-opened @2501 – Matt Jun 08 '16 at 07:29
  • @DmitriChubarov Isn't this used as dupe right now? – πάντα ῥεῖ Jun 08 '16 at 07:52
  • @πάνταῥεῖ I just wanted to get the post linked for those who could not see the close votes before it was actually closed. – Dima Chubarov Jun 08 '16 at 10:37

0 Answers0