Reputation: 183
In an if statement like this:
if(strcmp(str1,str2))
strcmp() can return a negative value, and if it does, does the if statement consider it to be TRUE or FALSE.
Upvotes: 0
Views: 8572
Reputation: 37206
The strcmp
function when used with an if statement can lead to some very unreadable code. The following statement says to call foo
if and only if str1
is different than str2
.
if (strcmp(str1, str2)) foo();
Some would argue this is somewhat unexpected and unreadable, but it's due to the fact that strcmp
is not really meant to be used this way as it does not return a bool
. If you check out this great reference you'll see that it returns an integral value which is meant to indicate the relationship between the strings. strcmp
can tell you much more than simply whether or not two strings are the same. According to the reference strcmp
returns:
str1
than in str2
str2
than in str1
Because an if statement will coerce any non-zero integer value into a boolean value of true
the if statement if (strcmp(str1, str2)) foo()
will always interpret true
and execute foo
, except when str1
and str2
are equal (in which case the if statement will interpret false
and foo will not be executed).
The more common way to use strcmp with an if statement is to combine your call to strcmp with a binary comparison within the if statement, testing against an integral value:
if (strcmp(str1, str2) == 0) foo(); // foo executed iff str1 and str2 are equal
if (strcmp(str1, str2) != 0) foo(); // foo executed iff str1 and str2 are not equal
You might also use strcmp
with an if statement to sort strings, strcmp is ideal for this because it returns based on the first unmatching character of the string. You could use it with something like the following (untested code):
bool swapped = false;
do {
for (i = 1; i < numStrings; i++) {
if (strcmp(str[i-1], str[i]) > 0) {
swap(i-1, i);
swapped = true;
}
}
} while (swapped);
Upvotes: 1
Reputation: 129454
C converts any non-zero value to true
and only zero is false
.
Note that if(strcmp(str1, str2))
is NEARLY always "wrong". You either want one of:
if (strcmp(str1, str2) == 0)
to detect that two strings are equalif (strcmp(str1, str2) > 0)
to detect that str1 > str2
if (strcmp(str1, str2) < 0)
to detect that str1 < str2
The only reason for if(strcmp(str1, str2))
is for detecting that str1
is different from str2
- which is nearly always not what you want.
And of coruse, in C++, you probably shouldn't be using strcmp
at all - since you have std::string
that you can just write what you want as a comparison, e.g. if (str1 > str2)
or if (str1 != str2)
, and it's immediately obvious to most people what it means without further questions.
Upvotes: 0
Reputation: 76418
When an integer value is converted to a Boolean value, 0 is false and any other value is true.
Upvotes: 0
Reputation: 993861
In C++, the if
statement treats any nonzero value as true. A negative value is not zero, so it will be considered true. The following two statements are equivalent:
if (strcmp(str1, str2))
if (strcmp(str1, str2) != 0)
Upvotes: 12