By Bilow


2015-10-12 22:40:39 8 Comments

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.

2 comments

@Geza Lore 2015-10-12 22:58:35

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

@ergonaut 2015-10-12 22:52:59

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.

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] Is this the only return value for strcmp() in C?

  • 2014-07-29 12:19:35
  • Manish Giri
  • 2367 View
  • 2 Score
  • 4 Answer
  • Tags:   c string strcmp

7 Answered Questions

[SOLVED] When will strcmp not return -1, 0 or 1?

  • 2012-11-26 19:41:30
  • beatgammit
  • 7736 View
  • 2 Score
  • 7 Answer
  • Tags:   c strcmp

9 Answered Questions

[SOLVED] Difference between InvariantCulture and Ordinal string comparison

4 Answered Questions

[SOLVED] I do not understand strcmp results

  • 2015-03-15 14:21:33
  • user3540997
  • 1148 View
  • 2 Score
  • 4 Answer
  • Tags:   c strcmp string.h

4 Answered Questions

[SOLVED] What does strcmp return if two similar strings are of different lengths?

  • 2016-04-09 15:33:34
  • Daniel Soutar
  • 14760 View
  • 12 Score
  • 4 Answer
  • Tags:   c strcmp

1 Answered Questions

[SOLVED] C strcmp() not returning 0 as expected

  • 2017-06-06 22:11:57
  • Ostküste
  • 618 View
  • 1 Score
  • 1 Answer
  • Tags:   c debugging strcmp

1 Answered Questions

[SOLVED] strcmp output with positive value

  • 2015-09-17 03:23:22
  • SpaceWalker
  • 275 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ strcmp

Sponsored Content