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:
- either you have reached the last character of both strings (if they are the same size)
- 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)