I assume that you are using GCC when compiling this, I tried it on 4.8.4. The trick here is that GCC understands the semantics of some standard library functions ( strcmp is one of them). In your case, the compiler will completely eliminate the second call to strcmp , because it knows that the strcmp result of the given string constants "0" and "9" will be negative, and the standard compatible value (-1) will be instead of making the call. It cannot do the same with the first call, because s1 and s2 can be changed in memory (imagine an interrupt or multiple threads, etc.).
You can do an experiment to test this. Add the const qualifier to the arrays so that GCC knows that they cannot be changed:
const char s1[] = "0"; const char s2[] = "9"; printf("%d\n", strcmp(s1, s2));
You can also look at the compiler output collector (use the -S flag).
The best way to check, however, is to use -fno-builtin , which disables this optimization. With this option, your source code will print -9 in both cases
Geza lore
source share