# Thread: if(strcmp(string1, string2)) Equivalent to if(strcmp(string1, string2) == 0)

1. ## if(strcmp(string1, string2)) Equivalent to if(strcmp(string1, string2) == 0)

Is this statement:

if(strcmp(string1, string2))

Equivalent to

if(strcmp(string1, string2) == 0)

My understanding is case 1 is the correct way to execute an if statement if two strings match. In my code however, case 1 produces an error and case 2 gives the behaviour that I want.

This is an SSCE excerpt. My entire codebase is pretty large. 2. No, you are mistaken:
Code:
```if (A)
// is equivalent to
if (A != 0)```
In C, zero is false and everything else is true. 3. Originally Posted by GReaper No, you are mistaken:
Code:
```if (A)
// is equivalent to
if (A != 0)```
In C, zero is false and everything else is true.
Actually, you are partially mistaken.
Code:
`if(a) { ... }`
Yes, if a != 0 then a is "true", and if a == 0, then a is "false", in this expression.

Code:
`if(strcmp(s1, s2))`
However, your final statement is not always true.

if you "man strcmp", then you will see:

s1 is less than s2, strcmp() returns an int less than 0
s1 is equal to s2, strcmp() returns 0
s1 is greater than s2, strcmp() returns an int greater than 0

In the OP, the two statements are not the same. Plus you really need to code to handle the three cases. 4. However, your final statement is not always true.
In what way? Yes, you went on to say that strcmp's return value has a complex meaning, but that doesn't take away from the fact that integer value 0 is a Boolean false, and other integer values are Boolean true, which would make the statement you objected to... true.

In the OP, the two statements are not the same. Plus you really need to code to handle the three cases.
Perhaps this is true if you think, for example, that if(strcmp(s1,s2) == 0) ... handles all three cases. It's the same as saying that s1 being lexicographically equal to s2 is right and other results are wrong. In my experience, being interested in one result of strcmp is the overwhelming general case. Popular pages Recent additions case, equivalent, ifstrcmpstring1, statement, string2 