The two string 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() return zero.
If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.
(Compare ASCII values of string's characters)
if both string equal = 0
if first string > second string = +ve
if first string < second string = -ve
Note: strcmp() is case sensitive.
[Here, first string's first character(i.e. A) is ASCII value is 65 and in second string's first character(i.e. B) ASCII value is 66 so first iteration result is 0, now second iteration start now compare A and B (i.e. 65 and 66] so A<B (i.e. 65<66) so result is negative.]
illustrate strcmp() C program:
/*c program for illustrate strcmp() function*/
printf("Enter first string : ");
printf("Enter second string : ");
r = strcmp(str1,str2);
printf("Result : %d",r);
The output of above program would be:
|Figure: Screen shot for strcmp() when both string are equal C program|
|Figure: Screen shot for strcmp() when first string < second string|
|Figure: Screen shot for strcmp() when first string > second string|
- List of standard library function