title: Is this the only return value for strcmp() in C?
body: <p>I'm learning C, and am currently studying String Handling. From where I'm studying, <code>strcmp()</code> is defined as- </p>
  
  <blockquote>
    <p>This is a function which compares two strings to find out whether they
    are same or different. The two strings are compared character by
    character until there is a mismatch or end of one of the strings is
    reached, whichever occurs first. If the two strings are identical,
    strcmp( ) returns a value zero. If they’re not, it returns the numeric
    difference between the ASCII values of the first non-matching pairs of
    characters.</p>
  </blockquote>
  
  <p>There is a sample program given, which is what my question is about- </p>
  
  <pre><code>main( )
  {
      char string1[ ] = "Jerry" ;
      char string2[ ] = "Ferry" ;
      int i, j, k ;
      i = strcmp ( string1, "Jerry" ) ;
      j = strcmp ( string1, string2 ) ;
      k = strcmp ( string1, "Jerry boy" ) ;
      printf ( "\n%d %d %d", i, j, k ) ;
  }
  </code></pre>
  
  <p>I ran this program on Dev-C++ on my windows(64 bit) machine, and got this output-  <code>0 1 -1</code></p>
  
  <p>Now, the book gives the output as <code>0 4 -32</code>, with this reasoning- </p>
  
  <blockquote>
    <p>In the first call to strcmp( ), the two strings are identical—“Jerry”
    and “Jerry”—and the value returned by strcmp( ) is zero. In the second
    call, the first character of “Jerry” doesn't match with the first
    character of “Ferry” and the result is 4, which is the numeric
    difference between ASCII value of ‘J’ and ASCII value of ‘F’. In the
    third call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”,
    because the null character at the end of “Jerry” doesn’t match the
    blank in “Jerry boy”. The value returned is -32, which is the value of
    null character minus the ASCII value of space, i.e., ‘\0’ minus ‘ ’,
    which is equal to -32.</p>
  </blockquote>
  
  <p>To confirm what the book says, I added this code to my program, just to verify the ASCII difference between J and F: </p>
  
  <pre><code>printf("\n Ascii value of J is %d", 'J' );
  printf("\n Ascii value of F is %d", 'F' );
  </code></pre>
  
  <p>and then I got this in the output accordingly- </p>
  
  <pre><code> Ascii value of J is 74
   Ascii value of F is 70
  </code></pre>
  
  <p>This is according to what the book says, however, as you can see, I get different values of j and k, that is, when the strings don't match. I did look up for similar questions on SO, and got some of them, but could not come across a definite answer for the different output(when it returns <code>1 and -1</code>), hence I decided to ask a new question. </p>
  
  <p>This question <a href="https://stackoverflow.com/questions/13571907/when-will-strcmp-not-return-1-0-or-1?rq=1">here</a> seems to be somewhat similar, and the question description contains the following information about <code>strcmp()</code>-</p>
  
  <blockquote>
    <p>The strcmp() and strncmp() functions return an integer less than,
    equal to, or greater than zero if s1 (or the first n bytes thereof) is
    found, respectively, to be less than, to match, or be greater than s2</p>
  </blockquote>
  
  <p>In one of the answers, I came across <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/strcmp.html" rel="nofollow noreferrer">this link</a> which documents the functions of <code>strcmp()</code>. It further says- </p>
  
  <blockquote>
    <p>The strcmp() function shall compare the string pointed to by s1 to the
    string pointed to by s2.</p>
    
    <p>The sign of a non-zero return value shall be determined by the sign of
    the difference between the values of the first pair of bytes (both
    interpreted as type unsigned char) that differ in the strings being
    compared.</p>
    
    <p>RETURN VALUE</p>
    
    <p>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.</p>
  </blockquote>
  
  <p>So, after reading all this, I'm inclined to think that irrespective of the implementation/platform being used, the <code>strcmp()</code> function should be used only to consider the return value as being to one of three categories (<code>0, positive and negative</code>), instead of relying on the exact value being returned.</p>
  
  <p>Am I correct in my understanding?</p>