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

int main()
    int n = strcmp("hello","help");
    printf("%d\n", n ) ;
    return 0;



Does the value for this program have to be the same on all machines or different compilers?

In other words can this return value take on different values for the same program when run on different compilers or different machines?

  • 1
    Yes. Why do you suspect otherwise? – user40980 Feb 1 '14 at 5:01
  • @MichaelT I, at least, have heard that the only requirements for strcmp is that it returns negative, zero, and positive - but the particulars are up to implementation. (That is, it could be as simple as -1, 0, and 1, or the magnitudes could be some measure of difference between the strings) – Izkata Feb 1 '14 at 5:49
  • @Izkata my apologies for the ambiguity - there are two question marks in the question, and they have opposite answers. – user40980 Feb 1 '14 at 6:25

No, only its sign is specified.

C99 § The strcmp function


#include <string.h>
int strcmp(const char *s1, const char *s2);


The strcmp function compares the string pointed to by s1 to the string pointed to by s2.

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

| improve this answer | |
  • Is the meaning of "greater than" specified? Could "AA" > "Z" because the length is bigger, for example? – Móż Feb 1 '14 at 6:22
  • 1
    It's specified elsewhere that the comparison is between the first non-equal characters in the string, yes. – U2EF1 Feb 1 '14 at 7:48
  • @Ӎσᶎ "A" > "Z" could be true if an unusual character encoding was used. (non-ASCII) Otherwise with ASCII, both "AB" > "Z" and "A" > "Z" are false as 'A' < 'Z'. – chux - Reinstate Monica Feb 1 '14 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.