Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how the output 118 in this small code!

+1 vote
asked Apr 29, 2020 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, 2020 by mark (360 points)
where are the headers
commented Apr 30, 2020 by udhayakumar (150 points)
#include<stdio.h>
0 votes
answered May 5, 2020 by sofibilal193 (620 points)
You need to add two headers

#include<stdio.h>

#include<string.h>
0 votes
answered May 5, 2020 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, 2020 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.
...