how the output 118 in this small code!

+1 vote
asked Apr 29 by Vasanth kumar (130 points)
"int main()
{
char str1[5]="vector", str2[5]="vectoa";
int i;
i=strcmp(str1,str2);
printf("i = %d",i);
return 0;
}

Output: i = 118.
How the i value is getting 118?"

4 Answers

0 votes
answered Apr 30 by mark (360 points)
where are the headers
commented Apr 30 by udhayakumar (150 points)
#include<stdio.h>
0 votes
answered May 5 by sofibilal193 (580 points)
You need to add two headers

#include<stdio.h>

#include<string.h>
0 votes
answered May 5 by arunthesun (180 points)
1. strings are 6 character long but initialized to 5. Correct initialization would be str1[] = "vector", str2[]="vectoa";

2. Expected behaviour of strcmp. strcmp returns the distance between first difference between two strings.

strcmp(str1, str2) for strings defined as in #1 would be 17 (ascii (r) - ascii(a)).

for str1[] = "vector", str2[] = "vectop", strcmp(str1, str2) will return 2 (ascii (r) - ascii(p))

As for i = 118, because of comparing unintended values (could be ascii(v) - ascii(NULL) ??)
0 votes
answered May 6 by udhayakumar (150 points)
internally working code is the answer
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...