Search code examples
c++strcmp

If statement with negative values


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.


Solution

  • 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)