-2

I'm fairly new to c and my understanding is that you cant subtract array indexes as you do in languages like python.

The part I don't understand:

s1[i] - s2[i]

the code:

int strcmp(char *s1, char *s2)
{
    int i;

    i = 0;
    while (s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i])
        i++;
    return (s1[i] - s2[i]);
}

Edit: I feel fairly dumb and ashamed for asking such a (in retrospect) dumb question.

Miguel M
  • 302
  • 3
  • 16
  • Returns the difference of the (ascii) values of the ith characters of the string. – alex01011 Jul 20 '21 at 16:10
  • 4
    If you compare the strings "black" and "blank", it will end up returning the value `'c' - 'n'`, or in ASCII `99 - 110`, which will tell you which string is "greater" -- that is, you get a negative/zero/positive result according to whether the first string is "less than", equal to, or "greater than" the second. – Steve Summit Jul 20 '21 at 16:12
  • If you're asking a question about a modification to the code shown, then you should include your proposed modified code so we know for sure what you're asking about. – jarmod Jul 20 '21 at 16:13
  • 1
    It returns a positive or negative number if the two characters are different, and zero if they the same. Just like the standard [`strcmp` returns](https://en.cppreference.com/w/c/string/byte/strcmp#Return_value). – Some programmer dude Jul 20 '21 at 16:13
  • 1
    I feel like all the comments here are doing OP a great disservice. Instead of giving OP an answer, we should be asking what is unclear and teach OP code reading skills. – SergeyA Jul 20 '21 at 16:15
  • 1
    and up to that index the strings are the same, so you'll get which one was lexicographically greater. If the index reached the NUL terminators, then subtracting them will be `0` so the function will return the 'equality' value `0`. – Weather Vane Jul 20 '21 at 16:16
  • 1
    @SergeyA I thought you couldn't do that subtraction with strings so that confused me I however forgot strings are arrays of chars and you could boil it down to 255 +/- 255 – Miguel M Jul 20 '21 at 16:22
  • ...and if only one NUL terminator was found before the other, the strings are not the same and the subtraction is non-0 signifying inequality. The "characters" are integers. – Weather Vane Jul 20 '21 at 16:22
  • [Is there any compiler and library where strcmp() returns values other than -1 0 and 1?](https://stackoverflow.com/q/59779056/995714), [When will strcmp not return -1, 0 or 1?](https://stackoverflow.com/q/13571907/995714) – phuclv Jul 20 '21 at 17:11

1 Answers1

1

In C compiler everything is treated with its ASCII representation. For example, if you have a string let's say a b c d e in compiler it would be reflected with 97 98 99 100 101 for more

Now in your program, you are iterating two strings until either one of them (or both) comes to the last index or they are equal.

So eventually when the loop stops:

  1. either you have reached the last character of both strings (if they are the same size)
  2. when any mismatch is found between characters of the two strings,

In any of the scenario when you are returning s1[i] - s2[i] that actually would return the int value of ascii(any character of[s1]) - ascii(any character of[s2])

For example -

Let's say you have two strings:

s1 = "abcde";
s2 = "abdef";

so the loop will break at index 2, and it would return ascii(c) - ascii(d) => 99 - 100 => -1.

Note: it would help you to understand which string (s1/s2) is greater/lower than the other. (or even they are same or not)

Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17