How this get result -1?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How this get result -1??

What is the output of this code? #include <stdio.h> int main() { int b, p; char str1[] = "Sololearn"; char str2[] = "sololearn"; b = strcmp(str1, str2); printf("%d", b); return 0; }

3rd Jun 2021, 3:03 PM
Soharab Uddin Mondal
Soharab Uddin Mondal - avatar
4 Answers
+ 3
soharab uddin mondal strcmp compares a string character by character , if the first character of both string are equal (i.e. have same ascii value), it will return 0 . If a character of first string has a ascii value less than character of second string ,then it will return -1 and vice versa . Here "S" Has ascii value of 83 , while "s" has a value of 115 .
3rd Jun 2021, 3:14 PM
Abhay
Abhay - avatar
+ 1
Ascii value of S is smaller than s
3rd Jun 2021, 3:05 PM
Abhay
Abhay - avatar
+ 1
Abhay Thank you so much. Your explanation was amazing. 👍👍👍
3rd Jun 2021, 4:00 PM
Soharab Uddin Mondal
Soharab Uddin Mondal - avatar
0
Abhay can you please explain in details ?
3rd Jun 2021, 3:08 PM
Soharab Uddin Mondal
Soharab Uddin Mondal - avatar