Jake
Jake

Reputation: 16837

C strcmp() with one char difference

My question is that how will strcmp() handle the following case:

strcmp("goodpassT", "goodpass");

I read that the comparison continues until a different character is found or null character (\0) is found in any of the strings. In the above case, when it encounters \0 for the second argument, will it just stop comparison, or will it still compare to the T character ? The return value is 1, but I'm not sure about the stopping condition.

Upvotes: 0

Views: 105

Answers (2)

Priyanka Nigam
Priyanka Nigam

Reputation: 14

The answer for this function strcmp("goodpassT", "goodpass"); will be 1 only.The point upto which lengths of both the string are same will be compared on the basis of their ASCII value.

Upvotes: 0

tstanisl
tstanisl

Reputation: 14127

The comparison is done using unsigned char. Thus the shorter string is smaller as its terminating 0 is smaller than other unsigned nonzero char in the longer string. See http://port70.net/~nsz/c/c11/n1570.html#7.24.4p1

Upvotes: 2

Related Questions